sid88in / serverless-appsync-plugin

serverless plugin for appsync
MIT License
952 stars 182 forks source link

Invalid GraphQL schema: There can be only one directive named "@aws_cognito_user_pools". #590

Open kuda1992 opened 1 year ago

kuda1992 commented 1 year ago

Hey there

I am using app-sync directives inside my graphql schema

directive @aws_cognito_user_pools on FIELD | OBJECT | INPUT_FIELD_DEFINITION | INPUT_OBJECT | MUTATION | ENUM | OBJECT | INTERFACE | UNION | SCALAR

schema {
    query: Query
    mutation: Mutation
}

type Account @aws_cognito_user_pools {
    AccountHoldersName: String
    AccountNo: String
    BuildingSocReference: String
    SortCode: String
}

However when I upload this using the serverless framework I am getting error

Error: Invalid GraphQL schema:
     There can be only one directive named "@aws_cognito_user_pools".

This used to work with the previous v1. Do you have an idea what the problem might be

fetree commented 1 year ago

Running into this problem as well with globbing

There can be only one directive named "@aws_iam". There can be only one directive named "@aws_api_key". There can be only one directive named "@aws_oidc".

kuda1992 commented 1 year ago

Hi @bboure

In relation to the issue above I was wondering if you have managed to look it. I debugged the code and have found the offending code.

src/resources/Schema.ts

inside the

generateSchema() {
    const schemaFiles = flatten(globby.sync(this.schemas));
    const schemas = schemaFiles.map((file) => {
      return fs.readFileSync(
        path.join(this.api.plugin.serverless.config.servicePath, file),
        'utf8',
      );
    });

    this.valdiateSchema(AWS_TYPES + '\n' + schemas.join('\n'));

    // Return single files as-is.
    if (schemas.length === 1) {
      return schemas[0];
    }

    // AppSync does not support Object extensions
    // https://spec.graphql.org/October2021/#sec-Object-Extensions
    // Merge the schemas
    return print(
      mergeTypeDefs(schemas, {
        forceSchemaDefinition: false,
        useSchemaDefinition: false,
        sort: true,
        throwOnConflict: true,
      }),
    );
  }

You're are prepending the AWS_TYPES to the schema which is fine if you don't have these types defined in your schema. However if you have lint on your schema you will have errors if you don't define the AWS_TYPES. Can you make including types optional or remove them.

bboure commented 1 year ago

You should not add those declarations in your schema, they are internal to AppSync and should not be uploaded. I understand that you're doing this for lining purposes. I also add them in the schema to validate them, then I remove them (They never hit AppSync).

I'd try to see if you can configure your linter to use external declarations somewhere.

What are you using?