diff --git a/website/pages/api-v16/error.mdx b/website/pages/api-v16/error.mdx index 09f5e746a6..50cb70e4ea 100644 --- a/website/pages/api-v16/error.mdx +++ b/website/pages/api-v16/error.mdx @@ -10,8 +10,7 @@ The `graphql/error` module is responsible for creating and formatting GraphQL errors. You can import either from the `graphql/error` module, or from the root `graphql` module. For example: ```js -import { GraphQLError } from 'graphql'; // ES6 -const { GraphQLError } = require('graphql'); // CommonJS +import { GraphQLError } from 'graphql'; ``` ## Overview diff --git a/website/pages/api-v16/execution.mdx b/website/pages/api-v16/execution.mdx index 4723513d23..c160797aa0 100644 --- a/website/pages/api-v16/execution.mdx +++ b/website/pages/api-v16/execution.mdx @@ -10,8 +10,7 @@ The `graphql/execution` module is responsible for the execution phase of fulfilling a GraphQL request. You can import either from the `graphql/execution` module, or from the root `graphql` module. For example: ```js -import { execute } from 'graphql'; // ES6 -const { execute } = require('graphql'); // CommonJS +import { execute } from 'graphql'; ``` ## Overview diff --git a/website/pages/api-v16/graphql-http.mdx b/website/pages/api-v16/graphql-http.mdx index 73c36fd310..9b8285cd6c 100644 --- a/website/pages/api-v16/graphql-http.mdx +++ b/website/pages/api-v16/graphql-http.mdx @@ -11,8 +11,7 @@ The [official `graphql-http` package](https://github.com/graphql/graphql-http) p ## Express ```js -import { createHandler } from 'graphql-http/lib/use/express'; // ES6 -const { createHandler } = require('graphql-http/lib/use/express'); // CommonJS +import { createHandler } from 'graphql-http/lib/use/express'; ``` ### createHandler diff --git a/website/pages/api-v16/graphql.mdx b/website/pages/api-v16/graphql.mdx index e6936f279c..2c736c87ff 100644 --- a/website/pages/api-v16/graphql.mdx +++ b/website/pages/api-v16/graphql.mdx @@ -10,8 +10,7 @@ The `graphql` module exports a core subset of GraphQL functionality for creation of GraphQL type systems and servers. ```js -import { graphql } from 'graphql'; // ES6 -const { graphql } = require('graphql'); // CommonJS +import { graphql } from 'graphql'; ``` ## Overview diff --git a/website/pages/api-v16/language.mdx b/website/pages/api-v16/language.mdx index 897bb00927..cd96ce4101 100644 --- a/website/pages/api-v16/language.mdx +++ b/website/pages/api-v16/language.mdx @@ -9,8 +9,7 @@ title: graphql/language The `graphql/language` module is responsible for parsing and operating on the GraphQL language. You can import either from the `graphql/language` module, or from the root `graphql` module. For example: ```js -import { Source } from 'graphql'; // ES6 -const { Source } = require('graphql'); // CommonJS +import { Source } from 'graphql'; ``` ## Overview diff --git a/website/pages/api-v16/type.mdx b/website/pages/api-v16/type.mdx index 4ab3d7d1a2..c829d9708d 100644 --- a/website/pages/api-v16/type.mdx +++ b/website/pages/api-v16/type.mdx @@ -9,8 +9,7 @@ title: graphql/type The `graphql/type` module is responsible for defining GraphQL types and schema. You can import either from the `graphql/type` module, or from the root `graphql` module. For example: ```js -import { GraphQLSchema } from 'graphql'; // ES6 -const { GraphQLSchema } = require('graphql'); // CommonJS +import { GraphQLSchema } from 'graphql'; ``` ## Overview diff --git a/website/pages/api-v16/utilities.mdx b/website/pages/api-v16/utilities.mdx index ba8533c220..1e646d8be7 100644 --- a/website/pages/api-v16/utilities.mdx +++ b/website/pages/api-v16/utilities.mdx @@ -10,8 +10,7 @@ The `graphql/utilities` module contains common useful computations to use with the GraphQL language and type objects. You can import either from the `graphql/utilities` module, or from the root `graphql` module. For example: ```js -import { introspectionQuery } from 'graphql'; // ES6 -const { introspectionQuery } = require('graphql'); // CommonJS +import { introspectionQuery } from 'graphql'; ``` ## Overview diff --git a/website/pages/api-v16/validation.mdx b/website/pages/api-v16/validation.mdx index 6b45caec6a..1acc121da6 100644 --- a/website/pages/api-v16/validation.mdx +++ b/website/pages/api-v16/validation.mdx @@ -10,8 +10,7 @@ The `graphql/validation` module fulfills the Validation phase of fulfilling a GraphQL result. You can import either from the `graphql/validation` module, or from the root `graphql` module. For example: ```js -import { validate } from 'graphql/validation'; // ES6 -const { validate } = require('graphql/validation'); // CommonJS +import { validate } from 'graphql/validation'; ``` ## Overview diff --git a/website/pages/docs/abstract-types.mdx b/website/pages/docs/abstract-types.mdx index fd92b0e3b6..607da1d309 100644 --- a/website/pages/docs/abstract-types.mdx +++ b/website/pages/docs/abstract-types.mdx @@ -36,7 +36,7 @@ concrete type a given value corresponds to. The following example defines a `ContentItem` interface for a publishing platform: ```js -const { GraphQLInterfaceType, GraphQLString, GraphQLNonNull } = require('graphql'); +import { GraphQLInterfaceType, GraphQLString, GraphQLNonNull } from 'graphql'; const ContentItemInterface = new GraphQLInterfaceType({ name: 'ContentItem', @@ -69,7 +69,7 @@ The following example implements the `Article` and `PodcastEpisode` types that conform to the `ContentItem` interface: ```js -const { GraphQLObjectType, GraphQLString, GraphQLNonNull } = require('graphql'); +import { GraphQLObjectType, GraphQLString, GraphQLNonNull } from 'graphql'; const ArticleType = new GraphQLObjectType({ name: 'Article', @@ -114,7 +114,7 @@ A union requires: The following example defines a `SearchResult` union: ```js -const { GraphQLUnionType } = require('graphql'); +import { GraphQLUnionType } from 'graphql'; const SearchResultType = new GraphQLUnionType({ name: 'SearchResult', @@ -134,7 +134,7 @@ const SearchResultType = new GraphQLUnionType({ }); ``` -Unlike interfaces, unions don’t declare any fields of their own. Clients use inline fragments +Unlike interfaces, unions don't declare any fields of their own. Clients use inline fragments to query fields from the concrete types. ## Resolving abstract types at runtime diff --git a/website/pages/docs/advanced-custom-scalars.mdx b/website/pages/docs/advanced-custom-scalars.mdx index 91a068409a..b71aa450fc 100644 --- a/website/pages/docs/advanced-custom-scalars.mdx +++ b/website/pages/docs/advanced-custom-scalars.mdx @@ -9,7 +9,7 @@ schema, follow these best practices. ### Document expected formats and validation -Provide a clear description of the scalar’s accepted input and output formats. For example, a +Provide a clear description of the scalar's accepted input and output formats. For example, a `DateTime` scalar should explain that it expects [ISO-8601](https://www.iso.org/iso-8601-date-and-time-format.html) strings ending with `Z`. Clear descriptions help clients understand valid input and reduce mistakes. @@ -100,8 +100,8 @@ describe('DateTime scalar', () => { Integrate the scalar into a schema and run real GraphQL queries to validate end-to-end behavior. ```js -const { graphql, GraphQLSchema, GraphQLObjectType } = require('graphql'); -const { DateTimeResolver as DateTime } = require('graphql-scalars'); +import { graphql, GraphQLSchema, GraphQLObjectType } from 'graphql'; +import { DateTimeResolver as DateTime } from 'graphql-scalars'; const Query = new GraphQLObjectType({ name: 'Query', @@ -184,13 +184,13 @@ scalars for DateTime, EmailAddress, URL, UUID, and many others. ### Example: Handling email validation Handling email validation correctly requires dealing with Unicode, quoted local parts, and -domain validation. Rather than writing your own regex, it’s better to use a library scalar +domain validation. Rather than writing your own regex, it's better to use a library scalar that's already validated against standards. If you need domain-specific behavior, you can wrap an existing scalar with custom rules: ```js -const { EmailAddressResolver } = require('graphql-scalars'); +import { EmailAddressResolver } from 'graphql-scalars'; const StrictEmailAddress = new GraphQLScalarType({ ...EmailAddressResolver, diff --git a/website/pages/docs/authentication-and-express-middleware.mdx b/website/pages/docs/authentication-and-express-middleware.mdx index 4052f7ee26..1d8c75e865 100644 --- a/website/pages/docs/authentication-and-express-middleware.mdx +++ b/website/pages/docs/authentication-and-express-middleware.mdx @@ -14,9 +14,9 @@ For example, let's say we wanted our server to log the IP address of every reque ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const { buildSchema } = require('graphql'); +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import { buildSchema } from 'graphql'; const schema = buildSchema(`type Query { ip: String }`); @@ -46,17 +46,17 @@ app.all( app.listen(4000); console.log('Running a GraphQL API server at localhost:4000/graphql'); -```` +``` ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const { +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import { GraphQLObjectType, GraphQLSchema, GraphQLString, -} = require('graphql'); +} from 'graphql'; const schema = new GraphQLSchema({ query: new GraphQLObjectType({ diff --git a/website/pages/docs/basic-types.mdx b/website/pages/docs/basic-types.mdx index 90f7c7f9cf..08fbe2a44e 100644 --- a/website/pages/docs/basic-types.mdx +++ b/website/pages/docs/basic-types.mdx @@ -17,9 +17,9 @@ Each of these types maps straightforwardly to JavaScript, so you can just return ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const { buildSchema } = require('graphql'); +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import { buildSchema } from 'graphql'; // Construct a schema, using GraphQL schema language const schema = buildSchema(` @@ -54,19 +54,19 @@ app.all( app.listen(4000); console.log('Running a GraphQL API server at localhost:4000/graphql'); -```` +``` ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const { +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import { GraphQLObjectType, GraphQLSchema, GraphQLString, GraphQLFloat, GraphQLList, -} = require('graphql'); +} from 'graphql'; // Construct a schema const schema = new GraphQLSchema({ @@ -99,8 +99,8 @@ app.all( app.listen(4000); console.log('Running a GraphQL API server at localhost:4000/graphql'); -```` +``` diff --git a/website/pages/docs/constructing-types.mdx b/website/pages/docs/constructing-types.mdx index 2062ed4dad..40bd1bfa87 100644 --- a/website/pages/docs/constructing-types.mdx +++ b/website/pages/docs/constructing-types.mdx @@ -13,9 +13,9 @@ For example, let's say we are building a simple API that lets you fetch user dat ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const { buildSchema } = require('graphql'); +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import { buildSchema } from 'graphql'; const schema = buildSchema(` type User { @@ -61,9 +61,9 @@ console.log('Running a GraphQL API server at localhost:4000/graphql'); ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const graphql = require('graphql'); +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import * as graphql from 'graphql'; // Maps id to User object const fakeDatabase = { diff --git a/website/pages/docs/cursor-based-pagination.mdx b/website/pages/docs/cursor-based-pagination.mdx index a5f628315d..e14cc9796d 100644 --- a/website/pages/docs/cursor-based-pagination.mdx +++ b/website/pages/docs/cursor-based-pagination.mdx @@ -237,7 +237,7 @@ The following example shows how to paginate a list of users using PostgreSQL and client like `pg`: ```js -const db = require('./db'); +import db from './db'; async function resolveUsers(_, args) { const limit = args.first ?? 10; diff --git a/website/pages/docs/custom-scalars.mdx b/website/pages/docs/custom-scalars.mdx index d724360e9b..043a729b29 100644 --- a/website/pages/docs/custom-scalars.mdx +++ b/website/pages/docs/custom-scalars.mdx @@ -19,7 +19,7 @@ full control over how values are serialized, parsed, and validated. Here’s a simple example of a custom scalar that handles date-time strings: ```js -const { GraphQLScalarType, Kind } = require('graphql'); +import { GraphQLScalarType, Kind } from 'graphql'; const DateTime = new GraphQLScalarType({ name: 'DateTime', @@ -89,7 +89,7 @@ The following example is a custom `DateTime` scalar that handles ISO-8601 encode date strings: ```js -const { GraphQLScalarType, Kind } = require('graphql'); +import { GraphQLScalarType, Kind } from 'graphql'; const DateTime = new GraphQLScalarType({ name: 'DateTime', diff --git a/website/pages/docs/getting-started.mdx b/website/pages/docs/getting-started.mdx index 16022bd22c..7a0286c890 100644 --- a/website/pages/docs/getting-started.mdx +++ b/website/pages/docs/getting-started.mdx @@ -11,7 +11,8 @@ import { Tabs } from 'nextra/components'; ## Prerequisites -Before getting started, you should have Node v6 installed, although the examples should mostly work in previous versions of Node as well. +Before getting started, you should have at least Node 20 installed, the examples can be tweaked to work with Node versions +before that by switching to require syntax. For this guide, we won't use any language features that require transpilation, but we will use some ES6 features like [Promises](http://web.dev/articles/promises/), classes, and arrow functions, so if you aren't familiar with them you might want to read up on them first. @@ -33,7 +34,7 @@ To handle GraphQL queries, we need a schema that defines the `Query` type, and w ```javascript -const { graphql, buildSchema } = require('graphql'); +import { graphql, buildSchema } from 'graphql'; // Construct a schema, using GraphQL schema language const schema = buildSchema(`type Query { hello: String } `); @@ -58,7 +59,7 @@ graphql({ ```javascript -const { graphql, GraphQLSchema, GraphQLObjectType, GraphQLString } = require('graphql'); +import { graphql, GraphQLSchema, GraphQLObjectType, GraphQLString } from 'graphql'; // Construct a schema const schema = new GraphQLSchema({ diff --git a/website/pages/docs/going-to-production.mdx b/website/pages/docs/going-to-production.mdx index 862932fb10..5d94132531 100644 --- a/website/pages/docs/going-to-production.mdx +++ b/website/pages/docs/going-to-production.mdx @@ -25,7 +25,6 @@ export default defineConfig({ ### Next.js ```js -// ... /** @type {import('next').NextConfig} */ const nextConfig = { webpack(config, { webpack }) { @@ -39,7 +38,7 @@ const nextConfig = { }, }; -module.exports = nextConfig; +export default nextConfig; ``` ### create-react-app @@ -47,8 +46,8 @@ module.exports = nextConfig; With `create-react-app`, you need to use a third-party package like [`craco`](https://craco.js.org/) to modify the bundler configuration. ```js -const webpack = require('webpack'); -module.exports = { +import webpack from 'webpack'; +export default { webpack: { plugins: [ new webpack.DefinePlugin({ diff --git a/website/pages/docs/mutations-and-input-types.mdx b/website/pages/docs/mutations-and-input-types.mdx index bf0e3925e6..7d45bbe6f4 100644 --- a/website/pages/docs/mutations-and-input-types.mdx +++ b/website/pages/docs/mutations-and-input-types.mdx @@ -22,11 +22,11 @@ type Query { ```js -const { +import { GraphQLObjectType, GraphQLString, GraphQLSchema, -} = require('graphql'); +} from 'graphql'; const schema = new GraphQLSchema({ query: new GraphQLObjectType({ @@ -95,14 +95,15 @@ type Mutation { ```js -const { +import { GraphQLObjectType, GraphQLString, GraphQLSchema, GraphQLID, GraphQLInputObjectType, GraphQLNonNull, -} = require('graphql'); +} from 'graphql'; +import { randomBytes } from 'node:crypto'; // Maps username to content const fakeDatabase = {}; @@ -156,7 +157,7 @@ const schema = new GraphQLSchema({ }, resolve: (_, { input }) => { // Create a random id for our "database". - const id = require('crypto').randomBytes(10).toString('hex'); + const id = randomBytes(10).toString('hex'); fakeDatabase[id] = input; return { id, @@ -203,9 +204,9 @@ Here's some runnable code that implements this schema, keeping the data in memor ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const { buildSchema } = require('graphql'); +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import { buildSchema } from 'graphql'; const fakeDatabase = {}; @@ -273,21 +274,20 @@ app.all( app.listen(4000, () => { console.log('Running a GraphQL API server at localhost:4000/graphql'); }); - ``` ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const { +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import { GraphQLObjectType, GraphQLString, GraphQLSchema, GraphQLID, GraphQLInputObjectType, GraphQLNonNull, -} = require('graphql'); +} from 'graphql'; // If Message had any complex fields, we'd put them on this object. class Message { @@ -350,7 +350,8 @@ const schema = new GraphQLSchema({ }, resolve: (_, { input }) => { // Create a random id for our "database". - const id = require('crypto').randomBytes(10).toString('hex'); + import { randomBytes } from 'crypto'; + const id = randomBytes(10).toString('hex'); fakeDatabase[id] = input; return { id, diff --git a/website/pages/docs/object-types.mdx b/website/pages/docs/object-types.mdx index ed0cfef5da..6b15c86646 100644 --- a/website/pages/docs/object-types.mdx +++ b/website/pages/docs/object-types.mdx @@ -18,14 +18,14 @@ type Query { ```js -const { +import { GraphQLObjectType, GraphQLNonNull, GraphQLInt, GraphQLString, GraphQLList, - GraphQLFloat, -} = require('graphql'); + GraphQLFloat +} from 'graphql'; new GraphQLObjectType({ name: 'Query', @@ -43,7 +43,7 @@ new GraphQLObjectType({ }, }, }) -```` +``` @@ -64,15 +64,15 @@ type Query { ```js -const { +import { GraphQLObjectType, GraphQLNonNull, GraphQLInt, GraphQLString, GraphQLList, GraphQLFloat, - GraphQLSchema, -} = require('graphql'); + GraphQLSchema +} from 'graphql'; const RandomDie = new GraphQLObjectType({ name: 'RandomDie', @@ -173,19 +173,19 @@ type RandomDie { type Query { getDie(numSides: Int): RandomDie } -```` +``` ```js -const { +import { GraphQLObjectType, GraphQLNonNull, GraphQLInt, GraphQLString, GraphQLList, GraphQLFloat, - GraphQLSchema, -} = require('graphql'); + GraphQLSchema +} from 'graphql'; const RandomDie = new GraphQLObjectType({ name: 'RandomDie', @@ -236,8 +236,7 @@ const schema = new GraphQLSchema({ } }) }); -```` - +``` @@ -246,9 +245,9 @@ Putting this all together, here is some sample code that runs a server with this ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const { buildSchema } = require('graphql'); +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import { buildSchema } from 'graphql'; // Construct a schema, using GraphQL schema language const schema = buildSchema(` @@ -299,20 +298,20 @@ app.all( ); app.listen(4000); console.log('Running a GraphQL API server at localhost:4000/graphql'); -```` +``` ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const { +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import { GraphQLObjectType, GraphQLNonNull, GraphQLInt, - GraphQLString, GraphQLList, GraphQLFloat, -} = require('graphql'); + GraphQLSchema +} from 'graphql'; const RandomDie = new GraphQLObjectType({ name: 'RandomDie', @@ -351,7 +350,7 @@ const schema = new GraphQLSchema({ type: RandomDie, args: { numSides: { - type: GraphQLInt, + type: GraphQLInt } }, resolve: (_, { numSides }) => { diff --git a/website/pages/docs/passing-arguments.mdx b/website/pages/docs/passing-arguments.mdx index 54d9489235..c1c010dde0 100644 --- a/website/pages/docs/passing-arguments.mdx +++ b/website/pages/docs/passing-arguments.mdx @@ -24,16 +24,16 @@ type Query { ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const { +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import { GraphQLObjectType, GraphQLSchema, GraphQLList, GraphQLFloat, GraphQLInt, - GraphQLNonNull, -} = require('graphql'); + GraphQLNonNull +} from 'graphql'; const schema = new GraphQLSchema({ query: new GraphQLObjectType({ @@ -107,9 +107,9 @@ The entire code for a server that hosts this `rollDice` API is: ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const { buildSchema } = require('graphql'); +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import { buildSchema } from 'graphql'; // Construct a schema, using GraphQL schema language const schema = buildSchema(/_ GraphQL _/ ` type Query { rollDice(numDice: Int!, numSides: Int): [Int] }`); @@ -139,17 +139,17 @@ console.log('Running a GraphQL API server at localhost:4000/graphql'); ```js -const express = require('express'); -const { createHandler } = require('graphql-http/lib/use/express'); -const { +import express from 'express'; +import { createHandler } from 'graphql-http/lib/use/express'; +import { GraphQLObjectType, GraphQLNonNull, GraphQLInt, GraphQLString, GraphQLList, GraphQLFloat, - GraphQLSchema, -} = require('graphql'); + GraphQLSchema +} from 'graphql'; // Construct a schema, using GraphQL schema language const schema = new GraphQLSchema({ diff --git a/website/pages/docs/running-an-express-graphql-server.mdx b/website/pages/docs/running-an-express-graphql-server.mdx index e9f03b0e11..cc9238b379 100644 --- a/website/pages/docs/running-an-express-graphql-server.mdx +++ b/website/pages/docs/running-an-express-graphql-server.mdx @@ -16,9 +16,9 @@ Let's modify our "hello world" example so that it's an API server rather than a ```javascript -const { buildSchema } = require('graphql'); -const { createHandler } = require('graphql-http/lib/use/express'); -const express = require('express'); +import { buildSchema } from 'graphql'; +import { createHandler } from 'graphql-http/lib/use/express'; +import express from 'express'; // Construct a schema, using GraphQL schema language const schema = buildSchema(`type Query { hello: String } `); @@ -49,9 +49,9 @@ console.log('Running a GraphQL API server at http://localhost:4000/graphql'); ```javascript -const { GraphQLObjectType, GraphQLSchema, GraphQLString } = require('graphql'); -const { createHandler } = require('graphql-http/lib/use/express'); -const express = require('express'); +import { GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql'; +import { createHandler } from 'graphql-http/lib/use/express'; +import express from 'express'; // Construct a schema const schema = new GraphQLSchema({ @@ -99,7 +99,7 @@ One easy way to add it to your server is via the MIT-licensed [ruru](https://git To do so, install the `ruru` module with `npm install --save ruru` and then add the following to your `server.js` file, then restart the `node server.js` command: ```js -const { ruruHTML } = require('ruru/server'); +import { ruruHTML } from 'ruru/server'; // Serve the GraphiQL IDE. app.get('/', (_req, res) => {