From c60bb2efc1624f4edbebe2c7776dbb2ab4331c5d Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Thu, 11 Jul 2019 16:39:31 +0300 Subject: [PATCH] Run prettier on all files (#546) --- .eslintrc.yml | 16 ++-- .nycrc.yml | 6 +- .travis.yml | 2 +- README.md | 232 +++++++++++++++++++++++++--------------------- examples/index.js | 13 ++- package.json | 4 +- 6 files changed, 147 insertions(+), 126 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index e918cb95..f834334e 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -31,7 +31,8 @@ rules: flowtype/require-readonly-react-props: off flowtype/require-return-type: off flowtype/require-types-at-top: off - flowtype/require-valid-file-annotation: [error, always, { annotationStyle: line, strict: true }] + flowtype/require-valid-file-annotation: + [error, always, { annotationStyle: line, strict: true }] flowtype/require-variable-type: off flowtype/sort-keys: off flowtype/spread-exact-type: off @@ -126,7 +127,7 @@ rules: no-implied-eval: error no-invalid-this: off no-iterator: error - no-labels: [error, {allowLoop: true}] + no-labels: [error, { allowLoop: true }] no-lone-blocks: error no-loop-func: error no-magic-numbers: off @@ -164,7 +165,7 @@ rules: require-await: error require-unicode-regexp: off vars-on-top: error - yoda: [error, never, {exceptRange: true}] + yoda: [error, never, { exceptRange: true }] # Strict Mode # https://eslint.org/docs/rules/#strict-mode @@ -183,7 +184,8 @@ rules: no-undef: error no-undef-init: error no-undefined: off - no-unused-vars: [error, {vars: all, args: after-used, argsIgnorePattern: "^_"}] + no-unused-vars: + [error, { vars: all, args: after-used, argsIgnorePattern: '^_' }] no-use-before-define: off # Node.js and CommonJS @@ -204,7 +206,7 @@ rules: # Stylistic Issues # https://eslint.org/docs/rules/#stylistic-issues - camelcase: [error, {properties: always}] + camelcase: [error, { properties: always }] capitalized-comments: off # maybe consistent-this: off func-name-matching: off @@ -212,7 +214,7 @@ rules: func-style: off id-blacklist: off id-length: off - id-match: [error, "^(?:_?[a-zA-Z0-9]*)|[_A-Z0-9]+$"] + id-match: [error, '^(?:_?[a-zA-Z0-9]*)|[_A-Z0-9]+$'] line-comment-position: off lines-around-comment: off lines-between-class-members: [error, always, { exceptAfterSingleLine: true }] @@ -343,7 +345,7 @@ rules: yield-star-spacing: off overrides: - - files: "resources/**" + - files: 'resources/**' parserOptions: sourceType: script rules: diff --git a/.nycrc.yml b/.nycrc.yml index c2d8d90e..fde5e5a0 100644 --- a/.nycrc.yml +++ b/.nycrc.yml @@ -1,8 +1,8 @@ include: - - "src/" + - 'src/' exclude: - - "src/polyfills" + - 'src/polyfills' clean: true -temp-directory: "coverage" +temp-directory: 'coverage' skip-full: true reporter: [lcov, html, text] diff --git a/.travis.yml b/.travis.yml index adb11130..9fbdd863 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ notifications: on_failure: change skip_join: true channels: - - "chat.freenode.net#graphql" + - 'chat.freenode.net#graphql' slack: secure: eFMk+9i9G0F8VYd0cd+qfBXEcngR1odYzGapdSSPH0Ag6Ck1sjK64n0iFwz+RDV2sSVnjnAA5qbXNcltBhEe1DkNsZVXOp4cDxKiFQOH1NJu9m29rYx+dZ4H93JjuDtH0u2o2op/PhtXlwAjr8GBcdGgFNTUbiJv9nIj4sYudQJFL8VUQwYviRO28xx12hG7cmKvQ7YZ/OKz52oGnIOM4yoQgPKFRY4ztMr6OrHwDwWMAxptZjKYY0i2hE3ot+OkWeWteZvE9E2Q0OJI7C6XlZg+bR6Mu7VS5s6jjL21luJJBgwihLuWVNMXxxEfoUCldo0s/RX8lObuFocKpnAtqVimr1rPZtUYZzy/Jjd1fxa27TgONZUgibWoRbAFDCdKhJOsBSLGfvQw+a3sxkV5zD5DKpgaNavT+GbOZJn4KGw3i1M/5Gp1tqB+RsTMjRvXQH5rsc+9z/flys1K9sNrc7O0NS2sVPayf4U5ECgT6t0y6NM1hQNepNHbvPSU/I/dUPYQEMcWMtdgtApAeUcbsRFUgFejNwp4hhTz0MS6k9AtmLJ79mNQBqDgZ3/pnW/YjW7BsK8/f6xOabxJjwIdLJvAifxRy/oj8wd5ttx8u+qKZ547PhdNJiMV7H60tGqkJshqbphlvWr48l0EV0A635uY24VefYoTeMIXZSeueME= diff --git a/README.md b/README.md index 1e7059aa..8ad1848b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -GraphQL HTTP Server Middleware -============================== +# GraphQL HTTP Server Middleware [![Build Status](https://travis-ci.org/graphql/express-graphql.svg?branch=master)](https://travis-ci.org/graphql/express-graphql) [![Coverage Status](https://coveralls.io/repos/graphql/express-graphql/badge.svg?branch=master&service=github)](https://coveralls.io/github/graphql/express-graphql?branch=master) @@ -33,15 +32,17 @@ const graphqlHTTP = require('express-graphql'); const app = express(); -app.use('/graphql', graphqlHTTP({ - schema: MyGraphQLSchema, - graphiql: true -})); +app.use( + '/graphql', + graphqlHTTP({ + schema: MyGraphQLSchema, + graphiql: true, + }), +); app.listen(4000); ``` - ## Setup with Restify Use `.get` or `.post` (or both) rather than `.use` to configure your route handler. If you want to show GraphiQL in the browser, set `graphiql: true` on your `.get` handler. @@ -52,70 +53,76 @@ const graphqlHTTP = require('express-graphql'); const app = restify.createServer(); -app.post('/graphql', graphqlHTTP({ - schema: MyGraphQLSchema, - graphiql: false -})); +app.post( + '/graphql', + graphqlHTTP({ + schema: MyGraphQLSchema, + graphiql: false, + }), +); -app.get('/graphql', graphqlHTTP({ - schema: MyGraphQLSchema, - graphiql: true -})); +app.get( + '/graphql', + graphqlHTTP({ + schema: MyGraphQLSchema, + graphiql: true, + }), +); app.listen(4000); ``` - ## Options The `graphqlHTTP` function accepts the following options: - * **`schema`**: A `GraphQLSchema` instance from [`GraphQL.js`][]. - A `schema` *must* be provided. +- **`schema`**: A `GraphQLSchema` instance from [`GraphQL.js`][]. + A `schema` _must_ be provided. - * **`graphiql`**: If `true`, presents [GraphiQL][] when the GraphQL endpoint is - loaded in a browser. We recommend that you set `graphiql` to `true` when your - app is in development, because it's quite useful. You may or may not want it - in production. - Alternatively, instead of `true` you can pass in an options object: - * **`defaultQuery`**: An optional GraphQL string to use when no query - is provided and no stored query exists from a previous session. - If undefined is provided, GraphiQL will use its own default query. +- **`graphiql`**: If `true`, presents [GraphiQL][] when the GraphQL endpoint is + loaded in a browser. We recommend that you set `graphiql` to `true` when your + app is in development, because it's quite useful. You may or may not want it + in production. + Alternatively, instead of `true` you can pass in an options object: - * **`rootValue`**: A value to pass as the `rootValue` to the `graphql()` - function from [`GraphQL.js/src/execute.js`](https://github.com/graphql/graphql-js/blob/master/src/execution/execute.js#L119). + - **`defaultQuery`**: An optional GraphQL string to use when no query + is provided and no stored query exists from a previous session. + If undefined is provided, GraphiQL will use its own default query. - * **`context`**: A value to pass as the `context` to the `graphql()` - function from [`GraphQL.js/src/execute.js`](https://github.com/graphql/graphql-js/blob/master/src/execution/execute.js#L120). If `context` is not provided, the - `request` object is passed as the context. +- **`rootValue`**: A value to pass as the `rootValue` to the `graphql()` + function from [`GraphQL.js/src/execute.js`](https://github.com/graphql/graphql-js/blob/master/src/execution/execute.js#L119). - * **`pretty`**: If `true`, any JSON response will be pretty-printed. +- **`context`**: A value to pass as the `context` to the `graphql()` + function from [`GraphQL.js/src/execute.js`](https://github.com/graphql/graphql-js/blob/master/src/execution/execute.js#L120). If `context` is not provided, the + `request` object is passed as the context. - * **`extensions`**: An optional function for adding additional metadata to the - GraphQL response as a key-value object. The result will be added to - `"extensions"` field in the resulting JSON. This is often a useful place to - add development time metadata such as the runtime of a query or the amount - of resources consumed. This may be an async function. The function is - given one object as an argument: `{ document, variables, operationName, result, context }`. +- **`pretty`**: If `true`, any JSON response will be pretty-printed. - * **`validationRules`**: Optional additional validation rules queries must - satisfy in addition to those defined by the GraphQL spec. +- **`extensions`**: An optional function for adding additional metadata to the + GraphQL response as a key-value object. The result will be added to + `"extensions"` field in the resulting JSON. This is often a useful place to + add development time metadata such as the runtime of a query or the amount + of resources consumed. This may be an async function. The function is + given one object as an argument: `{ document, variables, operationName, result, context }`. - * **`customValidateFn`**: An optional function which will be used to validate - instead of default `validate` from `graphql-js`. +- **`validationRules`**: Optional additional validation rules queries must + satisfy in addition to those defined by the GraphQL spec. - * **`customExecuteFn`**: An optional function which will be used to execute - instead of default `execute` from `graphql-js`. +- **`customValidateFn`**: An optional function which will be used to validate + instead of default `validate` from `graphql-js`. - * **`customFormatErrorFn`**: An optional function which will be used to format any - errors produced by fulfilling a GraphQL operation. If no function is - provided, GraphQL's default spec-compliant [`formatError`][] function will be used. +- **`customExecuteFn`**: An optional function which will be used to execute + instead of default `execute` from `graphql-js`. - * **`customParseFn`**: An optional function which will be used to create a document - instead of the default `parse` from `graphql-js`. +- **`customFormatErrorFn`**: An optional function which will be used to format any + errors produced by fulfilling a GraphQL operation. If no function is + provided, GraphQL's default spec-compliant [`formatError`][] function will be used. - * **`formatError`**: is deprecated and replaced by `customFormatErrorFn`. It will be - removed in version 1.0.0. +- **`customParseFn`**: An optional function which will be used to create a document + instead of the default `parse` from `graphql-js`. + +- **`formatError`**: is deprecated and replaced by `customFormatErrorFn`. It will be + removed in version 1.0.0. In addition to an object defining each option, options can also be provided as a function (or async function) which returns this options object. This function @@ -125,32 +132,34 @@ after the request has been parsed. The `graphQLParams` is provided as the object `{ query, variables, operationName, raw }`. ```js -app.use('/graphql', graphqlHTTP(async (request, response, graphQLParams) => ({ - schema: MyGraphQLSchema, - rootValue: await someFunctionToGetRootValue(request), - graphiql: true -}))); +app.use( + '/graphql', + graphqlHTTP(async (request, response, graphQLParams) => ({ + schema: MyGraphQLSchema, + rootValue: await someFunctionToGetRootValue(request), + graphiql: true, + })), +); ``` - ## HTTP Usage Once installed at a path, `express-graphql` will accept requests with the parameters: - * **`query`**: A string GraphQL document to be executed. +- **`query`**: A string GraphQL document to be executed. - * **`variables`**: The runtime values to use for any GraphQL query variables - as a JSON object. +- **`variables`**: The runtime values to use for any GraphQL query variables + as a JSON object. - * **`operationName`**: If the provided `query` contains multiple named - operations, this specifies which operation should be executed. If not - provided, a 400 error will be returned if the `query` contains multiple - named operations. +- **`operationName`**: If the provided `query` contains multiple named + operations, this specifies which operation should be executed. If not + provided, a 400 error will be returned if the `query` contains multiple + named operations. - * **`raw`**: If the `graphiql` option is enabled and the `raw` parameter is - provided raw JSON will always be returned instead of GraphiQL even when - loaded from a browser. +- **`raw`**: If the `graphiql` option is enabled and the `raw` parameter is + provided raw JSON will always be returned instead of GraphiQL even when + loaded from a browser. GraphQL will first look for each parameter in the URL's query-string: @@ -166,17 +175,16 @@ for `multipart/form-data` content, which may be useful for GraphQL mutations involving uploading files. See an [example using multer](https://github.com/graphql/express-graphql/blob/304b24b993c8f16fffff8d23b0fa4088e690874b/src/__tests__/http-test.js#L674-L741). If the POST body has not yet been parsed, express-graphql will interpret it -depending on the provided *Content-Type* header. +depending on the provided _Content-Type_ header. - * **`application/json`**: the POST body will be parsed as a JSON - object of parameters. +- **`application/json`**: the POST body will be parsed as a JSON + object of parameters. - * **`application/x-www-form-urlencoded`**: this POST body will be - parsed as a url-encoded string of key-value pairs. - - * **`application/graphql`**: The POST body will be parsed as GraphQL - query string, which provides the `query` parameter. +- **`application/x-www-form-urlencoded`**: this POST body will be + parsed as a url-encoded string of key-value pairs. +- **`application/graphql`**: The POST body will be parsed as GraphQL + query string, which provides the `query` parameter. ## Combining with Other Express Middleware @@ -192,12 +200,15 @@ const graphqlHTTP = require('express-graphql'); const app = express(); -app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }})); +app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 } })); -app.use('/graphql', graphqlHTTP({ - schema: MySessionAwareGraphQLSchema, - graphiql: true -})); +app.use( + '/graphql', + graphqlHTTP({ + schema: MySessionAwareGraphQLSchema, + graphiql: true, + }), +); ``` Then in your type definitions, you can access the request via the third "context" argument in your `resolve` function: @@ -210,13 +221,12 @@ new GraphQLObjectType({ type: GraphQLString, resolve(parentValue, args, request) { // use `request.session` here - } - } - } + }, + }, + }, }); ``` - ## Providing Extensions The GraphQL response allows for adding additional information in a response to @@ -237,22 +247,31 @@ const graphqlHTTP = require('express-graphql'); const app = express(); -app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }})); +app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 } })); -const extensions = ({ document, variables, operationName, result, context }) => { +const extensions = ({ + document, + variables, + operationName, + result, + context, +}) => { return { - runTime: Date.now() - context.startTime - } -} - -app.use('/graphql', graphqlHTTP(request => { - return { - schema: MyGraphQLSchema, - context: { startTime: Date.now() }, - graphiql: true, - extensions + runTime: Date.now() - context.startTime, }; -})); +}; + +app.use( + '/graphql', + graphqlHTTP(request => { + return { + schema: MyGraphQLSchema, + context: { startTime: Date.now() }, + graphiql: true, + extensions, + }; + }), +); ``` When querying this endpoint, it would include this information in the result, @@ -267,7 +286,6 @@ for example: } ``` - ## Additional Validation Rules GraphQL's [validation phase](https://graphql.github.io/graphql-spec/#sec-Validation) checks the query to ensure that it can be successfully executed against the schema. The `validationRules` option allows for additional rules to be run during this phase. Rules are applied to each node in an AST representing the query using the Visitor pattern. @@ -282,14 +300,14 @@ export function DisallowMetadataQueries(context) { Field(node) { const fieldName = node.name.value; - if (fieldName === "metadata") { + if (fieldName === 'metadata') { context.reportError( new GraphQLError( `Validation: Requesting the field ${fieldName} is not allowed`, ), ); } - } + }, }; } ``` @@ -307,10 +325,9 @@ const graphqlHTTP = require('express-graphql'); graphqlHTTP.getGraphQLParams(request).then(params => { // do something... -}) +}); ``` - ## Debugging Tips During development, it's useful to get more information from errors, such as @@ -321,13 +338,12 @@ customFormatErrorFn: error => ({ message: error.message, locations: error.locations, stack: error.stack ? error.stack.split('\n') : [], - path: error.path -}) + path: error.path, +}); ``` - -[`GraphQL.js`]: https://github.com/graphql/graphql-js -[`formatError`]: https://github.com/graphql/graphql-js/blob/master/src/error/formatError.js -[GraphiQL]: https://github.com/graphql/graphiql +[`graphql.js`]: https://github.com/graphql/graphql-js +[`formaterror`]: https://github.com/graphql/graphql-js/blob/master/src/error/formatError.js +[graphiql]: https://github.com/graphql/graphiql [`multer`]: https://github.com/expressjs/multer [`express-session`]: https://github.com/expressjs/session diff --git a/examples/index.js b/examples/index.js index d02ac2de..a2a2b9ce 100644 --- a/examples/index.js +++ b/examples/index.js @@ -15,10 +15,13 @@ var root = { }; var app = express(); -app.use('/graphql', graphqlHTTP({ - schema: schema, - rootValue: root, - graphiql: true, -})); +app.use( + '/graphql', + graphqlHTTP({ + schema: schema, + rootValue: root, + graphiql: true, + }), +); app.listen(4000); console.log('Running a GraphQL API server at http://localhost:4000/graphql'); diff --git a/package.json b/package.json index d57f1e5c..00a2b236 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,8 @@ "prepublish": ". ./resources/prepublish.sh", "test": "npm run lint && npm run check && npm run testonly", "testonly": "mocha src/**/__tests__/**/*.js", - "lint": "prettier --check 'src/**/*.js' 'resources/**/*.js' 'types/*.ts' && eslint src resources", - "prettier": "prettier --write 'src/**/*.js' 'types/*.ts'", + "lint": "prettier --ignore-path .gitignore --check '**/*.{js,ts,md,json,yml}' && eslint src resources", + "prettier": "prettier --ignore-path .gitignore --write '**/*.{js,ts,md,json,yml}'", "check": "flow check", "build": "rm -rf dist/* && babel src --ignore '**/__tests__' --out-dir dist && npm run build:flow", "build:flow": "find ./src -name '*.js' -not -path '*/__tests__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/dist\\//g'`.flow; done",