I missed this part and actually I had to come back to this part and learn about it. In fact I was trying to work with GraphQL query in my server and remove some stuff from it just so that I can do something else. And I was literally doing string manipulation and then it dawned on me that I am not the first person who needs these things so let's see how others are doing it.
Besides my approach felt like reinventing the wheel and TBF it also was not as safe as how folks at GraphQL-JS probably had done it.
import { parse } from 'graphql';
const query = /* GraphQL */ `
query {
getUsers(first: 10) {
id
}
}
`;
const ast = parse(query);
Find the complete implementation here
- Alternatively you can pass your schema definition to it.
- It will return an AST.
import { parse, buildASTSchema } from 'graphql';
const schemaString = /* GraphQL */ `
type User {
id: ID!
}
type Query {
getUsers(first: Int!): [User!]
}
`;
const ast = parse(schemaString);
const schema = buildASTSchema(ast);
- Takes an AST.
- Constructs a
GraphQLSchema
.
- Takes a
GraphQLSchema
and a GraphQL query. - Validates the query against the
GraphQLSchema
. - Returns an array of encountered errors.
- Returns an empty array if no errors were encountered and the document is valid.
- Stops validation after a
maxErrors
limit has been reached.- Protect us from attackers who send pathologically invalid queries to induce a DoS attack.
- By default
maxErrors
set to 100 errors.
- Takes a
GraphQLSchema
and a GraphQL query. - Invokes the resolvers of the query's fields.
- Resolvers should've been defined in the passed
GraphQLSchema
.
- Resolvers should've been defined in the passed
- Returns the response, or errors.
- Converts an AST into a string.
import { DocumentNode, parse, print } from 'graphql';
const query = /* GraphQL */ `
query {
getUsers(first: 10) {
id
}
}
`;
const ast = parse(query);
const definitionNode = (ast as DocumentNode).definitions[0];
print(definitionNode);
Returns the schema of a GraphQLSchema
instance in the form of a string.
- Most important function.
- Takes a
GraphQLSchema
instance and a GraphQL query. - Calls
validate
andexecute
. - AKA GraphQL engine.
Tip
Orchestrates the invocations of the resolver functions and package the response data according to the shape of the provided query.