How to use the graphql function from graphql

Find comprehensive JavaScript graphql.graphql code examples handpicked from public code repositorys.

graphql.graphql is a function in the graphql library that takes a schema and a query and returns the result of executing that query against the schema.

77
78
79
80
81
82
83
84
85
86
function introspect (text) {
  return new Promise(function (resolve, reject) {
    try {
      var astDocument = GraphQL.parse(text)
      var schema = GraphQL.buildASTSchema(astDocument)
      GraphQL.graphql({ schema, source: graphqlviz.query })
        .then(function (data) {
          resolve(data)
        })
        .catch(function (e) {
fork icon56
star icon682
watch icon14

94
95
96
97
98
99
100
101
102
103
}
var query = '{ hello'

agent.startTransaction('foo')

graphql.graphql(...buildGraphqlArgs({ schema, source: query, rootValue })).then(function (response) {
  t.ok(agent.currentSpan === null, 'no currentSpan .graphql().then(...)')
  agent.endTransaction()
  t.deepEqual(Object.keys(response), ['errors'])
  t.strictEqual(response.errors.length, 1, 'should have one error')
fork icon211
star icon536
watch icon296

+ 23 other calls in file

How does graphql.graphql work?

graphql.graphql is a function in the graphql library that executes a GraphQL query against a given schema. Here is the basic syntax of using graphql.graphql: javascript Copy code {{{{{{{ const { graphql } = require('graphql'); graphql(schema, query) .then((result) => { // handle the result of the query }); In this example, graphql.graphql is being called with a GraphQL schema and a query string as its two arguments. The schema argument is an instance of the GraphQLSchema class, which represents the entire GraphQL schema of the application. The query argument is a string representing the GraphQL query that will be executed against the schema. The graphql.graphql function then returns a promise that resolves to the result of executing the query against the schema. The result of the query is an object that contains the data returned by the query as well as any errors that occurred during execution. Overall, graphql.graphql provides a simple and convenient way to execute GraphQL queries against a given schema, making it a key part of any GraphQL server implementation.

61
62
63
64
65
66
67
68
69
70
      }
    }
  `;

try {
    let result = graphql.graphql(apiSchema, query);
    res.send(result);
} catch (err) {
    res.status(400).send(err);
}
fork icon43
star icon139
watch icon16

815
816
817
818
819
820
821
822
823
824
	query: { type: "string" },
	variables: { type: "object", optional: true },
},
async handler(ctx) {
	await this.prepareGraphQLSchema();
	return GraphQL.graphql(
		this.graphqlSchema,
		ctx.params.query,
		null,
		{ ctx },
fork icon54
star icon96
watch icon8

+ 4 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const { graphql, buildSchema } = require("graphql");

// Define a schema for a simple "Hello World" query
const schema = buildSchema(`
type Query {
hello: String
}
`);

// Define a resolver function for the "hello" field
const rootValue = {
  hello: () => "Hello World!",
};

// Execute the "hello" query against the schema
graphql(schema, "{ hello }", rootValue).then((result) => {
  console.log(result.data.hello); // Outputs "Hello World!"
});

In this example, we first define a GraphQL schema that includes a single field called "hello". We then define a resolver function for the "hello" field that returns the string "Hello World!". We then call graphql.graphql with the schema, the query { hello }, and the resolver function rootValue. The result of executing the query is returned as a promise, which we handle in the .then block by logging the value of the hello field in the query result. This is a simple example, but it demonstrates the basic mechanics of using graphql.graphql to execute GraphQL queries against a schema.

90
91
92
93
94
95
96
97
98
99
100
  }
`


async function main() {
  try {
    const result = await graphql({
      schema,
      source,
      rootValue,
    })
fork icon0
star icon0
watch icon1

+ 4 other calls in file

18
19
20
21
22
23
24
25
26
27
      pressure
      description
    }
  }
`;
const result = await graphql(schema, query);

// Assert
expect(result.errors).toBeUndefined();
expect(result.data.weather).toEqual(weatherData);
fork icon0
star icon0
watch icon1

+ 248 other calls in file

22
23
24
25
26
27
28
29
30
31
var outFile = path.resolve(__dirname, '__tests__', 'testschema.rfc.json');

var body = fs.readFileSync(inFile, 'utf8');
var ast = schema.parseSchemaIntoAST(body);
var astSchema = utilities.buildASTSchema(ast, 'Root', 'Mutation');
graphql.graphql(astSchema, utilities.introspectionQuery).then(
  function(result) {
    var out = JSON.stringify(result, null, 2);
    fs.writeFileSync(outFile, out);
  });
fork icon0
star icon0
watch icon2