GraphQL.js
GraphQL.js is the JavaScript reference implementation of the GraphQL specification. It provides a robust and flexible GraphQL runtime and query language parser, enabling developers to build GraphQL servers and clients. The current stable version is 16.13.2, with active development on the next major version 17.0.0, which frequently releases alpha versions and introduces breaking changes.
Common errors
-
Cannot query field "X" on type "Y". Did you mean "Z"?
cause The requested field 'X' does not exist on the specified type 'Y' in your GraphQL schema. This is often due to a typo in the query or a missing field definition in the schema.fixCheck your GraphQL schema for the correct field name on type 'Y', or verify your query for typos matching an existing field. -
Expected 'RootQueryType' to be an object type, got 'null'.
cause The schema definition is missing or incorrectly defining one of the root operation types (Query, Mutation, Subscription) as a non-object type or `null`.fixEnsure your `buildSchema` or schema definition correctly defines 'Query', 'Mutation', and 'Subscription' as object types (e.g., `type Query { ... }`). -
Variable "$variableName" of type "TypeName!" was provided invalid value.
cause A variable passed to the GraphQL query does not match the expected type defined in the schema, or a non-nullable variable was provided a `null` value.fixCheck the type definition of `$variableName` in your GraphQL query and schema, and ensure the provided value conforms to that type and nullability constraints. -
A field named "fieldName" already exists on type "TypeName"
cause Your GraphQL schema definition has duplicate field names on the same type, which is not allowed according to the GraphQL specification.fixRename or remove the duplicate field definitions in your GraphQL schema to ensure all fields on a given type have unique names.
Warnings
- deprecated The `assertValidExecutionArguments` utility function has been deprecated. While it still works, it's recommended to review its usage and consider more explicit validation within your application.
- gotcha When creating plain JavaScript objects intended purely as hash maps (e.g., for GraphQL values or configurations), using `Object.create(null)` is recommended over `{}` to avoid prototype pollution issues and unintended inherited properties from the default object prototype.
- breaking In v17 alpha releases, the direct `info.abortSignal` property in resolvers for execution cancellation has been replaced by a method `info.getAbortSignal()`. Direct access to the property will no longer work.
- breaking v17 alpha releases introduce stricter validation rules. This includes forbidding `@skip` and `@include` directives in subscription root selections, new validation for `@stream` directives applied to different instances of the same field, and rejection of deprecated fields when inferring input types.
Install
-
npm install graphql -
yarn add graphql -
pnpm add graphql
Imports
- graphql
const { graphql } = require('graphql');import { graphql } from 'graphql'; - buildSchema
const { buildSchema } = require('graphql');import { buildSchema } from 'graphql';
Quickstart
import { graphql, buildSchema } from 'graphql';
// Construct a schema, using GraphQL schema language
const schema = buildSchema(`
type Query {
hello: String
}
`);
// The root provides a resolver function for each API endpoint
const root = {
hello: () => {
return 'Hello world!';
},
};
// Run the GraphQL query '{ hello }' and print out the response
graphql({ schema, source: '{ hello }', rootValue: root }).then((response) => {
console.log(response);
});