apollographql / eslint-plugin-graphql

:vertical_traffic_light: Check your GraphQL query strings against a schema.
https://www.npmjs.com/package/eslint-plugin-graphql
1.21k stars 103 forks source link

How would you import directives/etc from AWS AppSync while using basic .graphql schema #263

Open 0xdevalias opened 4 years ago

0xdevalias commented 4 years ago

Using AWS AppSync you can export both a .graphql schema, and a full .json schema.

The .graphql is your basic actual definitions:

schema {
  query: AAAAAAAAAAA
}

type AAAAAAAAAAA {
  BBBBBBBB(CCCCCCCC: String): String
}

Whereas the .json includes all of the AWS specifics, as well as the basics from my .graphql schema:

Ideally I would like to be able to use the main .graphql file for simplicity, and include the AWS AppSync specifics from another external file (so that I don't get 'undefined directive' errors and similar)

When I just use the .graphql schema I end up with errors such as:

Error: Error while loading rule 'graphql/template-strings': Directive aws_subscribe: Couldn't find type aws_subscribe in any of the schemas.
Occurred while linting /Users/devalias/dev/REDACTED/schema.graphql
    at collectDirective (/Users/devalias/dev/REDACTED/node_modules/graphql-import/dist/definition.js:113:23)
    at Array.forEach (<anonymous>)
    at collectNode (/Users/devalias/dev/REDACTED/node_modules/graphql-import/dist/definition.js:105:25)
    at /Users/devalias/dev/REDACTED/node_modules/graphql-import/dist/definition.js:87:13
    at Array.forEach (<anonymous>)
    at collectNewTypeDefinitions (/Users/devalias/dev/REDACTED/node_modules/graphql-import/dist/definition.js:86:30)
    at Object.completeDefinitionPool (/Users/devalias/dev/REDACTED/node_modules/graphql-import/dist/definition.js:23:41)
    at Object.importSchema (/Users/devalias/dev/REDACTED/node_modules/graphql-import/dist/index.js:99:67)
    at Object.readSchema (/Users/devalias/dev/REDACTED/node_modules/graphql-config/lib/utils.js:137:51)
    at GraphQLProjectConfig.getSchema (/Users/devalias/dev/REDACTED/node_modules/graphql-config/lib/GraphQLProjectConfig.js:75:28)
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Not sure of a good source for the full AWS AppSync specifics.. but when googling for things these pages sounded interesting:

0xdevalias commented 4 years ago

As per https://github.com/awslabs/aws-mobile-appsync-sdk-js/issues/547#issuecomment-610671519, it seems we can get a minimal .graphql output of the AWS AppSync specific directives with:

npx graphql-introspection-json-to-sdl schema.json > aws-directives.graphql
"""This directive allows results to be deferred during execution"""
directive @defer on FIELD

"""
Tells the service this field/object has access authorized by an OIDC token.
"""
directive @aws_oidc on OBJECT | FIELD_DEFINITION

"""Directs the schema to enforce authorization on a field"""
directive @aws_auth(
  """List of cognito user pool groups which have access on this field"""
  cognito_groups: [String]
) on FIELD_DEFINITION

"""
Tells the service which subscriptions will be published to when this mutation is
called. This directive is deprecated use @aws_susbscribe directive instead.
"""
directive @aws_publish(
  """
  List of subscriptions which will be published to when this mutation is called.
  """
  subscriptions: [String]
) on FIELD_DEFINITION

"""
Tells the service this field/object has access authorized by a Cognito User Pools token.
"""
directive @aws_cognito_user_pools(
  """List of cognito user pool groups which have access on this field"""
  cognito_groups: [String]
) on OBJECT | FIELD_DEFINITION

"""Tells the service which mutation triggers this subscription."""
directive @aws_subscribe(
  """
  List of mutations which will trigger this subscription when they are called.
  """
  mutations: [String]
) on FIELD_DEFINITION

"""
Tells the service this field/object has access authorized by sigv4 signing.
"""
directive @aws_iam on OBJECT | FIELD_DEFINITION

"""
Tells the service this field/object has access authorized by an API key.
"""
directive @aws_api_key on OBJECT | FIELD_DEFINITION

I was then able to configure my .graphqlconfig as follows:

{
  "schemaPath": "schema.graphql",
  "includes": ["aws-directives.graphql"]
}

This appears to allow eslint to work correctly.

When I remove the "includes": ["aws-directives.graphql"], I get the error about the directives again (as expected):

⇒  yarn eslint 'schema.graphql'
yarn run v1.21.1
$ /Users/devalias/dev/REDACTED/node_modules/.bin/eslint schema.graphql
Error: Error while loading rule 'graphql/template-strings': Directive aws_subscribe: Couldn't find type aws_subscribe in any of the schemas.
Occurred while linting /Users/devalias/dev/REDACTED/schema.graphql
    at collectDirective (/Users/devalias/dev/REDACTED/node_modules/graphql-import/dist/definition.js:113:23)
    at Array.forEach (<anonymous>)
    at collectNode (/Users/devalias/dev/REDACTED/node_modules/graphql-import/dist/definition.js:105:25)
    at /Users/devalias/dev/REDACTED/node_modules/graphql-import/dist/definition.js:87:13
    at Array.forEach (<anonymous>)
    at collectNewTypeDefinitions (/Users/devalias/dev/REDACTED/node_modules/graphql-import/dist/definition.js:86:30)
    at Object.completeDefinitionPool (/Users/devalias/dev/REDACTED/node_modules/graphql-import/dist/definition.js:23:41)
    at Object.importSchema (/Users/devalias/dev/REDACTED/node_modules/graphql-import/dist/index.js:99:67)
    at Object.readSchema (/Users/devalias/dev/REDACTED/node_modules/graphql-config/lib/utils.js:137:51)
    at GraphQLProjectConfig.getSchema (/Users/devalias/dev/REDACTED/node_modules/graphql-config/lib/GraphQLProjectConfig.js:75:28)
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
0xdevalias commented 4 years ago

What isn't clear to me now, is how I would go about supporting this with the legacy graphql-config configuration (since that is the version this lib is pinned to), as it doesn't appear to include any documents type configuration:

0xdevalias commented 4 years ago

According to https://github.com/jimkyndemeyer/js-graphql-intellij-plugin/issues/114 it sounds like maybe I should make it a .graphqls file (rather than .graphql)? But I can't actually find any solid references on google as to why/what the difference is between those two extension types.