3

I am using serverless framework to deploy a graphql nodejs package to lambda function.

My current serverless.yml file involves with a POST method for all communication and also another one for playground which looks like below.

functions:
  graphql:
    handler: handler.server
    events:
      - http:
          path: /
          method: post
          cors: true
  playground:
    handler: handler.playground
    events:
      - http:
          path: /
          method: get
          cors: true

And my handler.ts looks like this.

const { GraphQLServerLambda } = require("graphql-yoga");
const {documentSubmissionMutation} = require('./mutations/documentMutation');
const {signUpMutation, whatever} = require('./mutations/signUpMutation');

const typeDefs = `
  type Query {
    hello(name: String): String!
  },
  type Mutation {
    signUp(
      email: String!
      password: String!
    ): String

    sendDocuments(
      user_id: String!
      documents: String!
    ): String!
  }
`

const resolvers = {
  Query : {
    hello : whatever
  },
  Mutation: {
    sendDocuments: documentSubmissionMutation,
    signUp: signUpMutation,
  }
}

const lambda = new GraphQLServerLambda({
    typeDefs,
    resolvers
});

exports.server = lambda.graphqlHandler;
exports.playground = lambda.playgroundHandler;

What I would like to do now is have 3 different paths. 1 for secure and 1 for public and 1 for admin. So basically the URL would be something like. localhost/public localhost/secre localhost/admin The secure path will use aws cognito pool to identify the API user api and and the other one would be open. The admin will use another aws cognito admin pool.

So first what I did was add it like this for a secure one.

const lambda = new GraphQLServerLambda({
    typeDefs,
    resolvers,
    context: req => ({ ...req })
});

const lambdaSecure = new GraphQLServerLambda({
  typeDefsSecure,
  resolversSecure,
  context: req => ({ ...req })
});


exports.server = lambda.graphqlHandler;
exports.playground = lambda.playgroundHandler;

exports.serverSecure = lambdaSecure.graphqlHandler;
exports.playgroundSecure = lambdaSecure.playgroundHandler;

Then in my serverless.yml file tried to put it like this.

functions:
  graphql:
    handler: handler.server
    events:
      - http:
          path: /
          method: post
          cors: true
  graphql:
    handler: handler.serverSecure
    events:
      - http:
          path: /
          method: post
          cors: true

  playground:
    handler: handler.playground
    events:
      - http:
          path: /
          method: get
          cors: true
  playground:
    handler: handler.playgroundSecure
    events:
      - http:
          path: /
          method: get
          cors: true

It din't work and threw an error duplicated mapping key in "/Users/nihit/Desktop/serverless/cvtre/serverless.yml" at line 50, column -135: graphql:

I tried it in different ways but I am not really sure which one is the right way to get two different URL paths.

1 Answer 1

1

The problem appears to be in your serverless.yml. In particular in the functions specification. The combination of path and method as well as the function name must be unique for each function.

So, the serverless.yml should look like:

functions:
  graphqlServer:
    handler: handler.server
    events:
      - http:
          path: server/public
          method: post
          cors: true
  graphqlServerSecure:
    handler: handler.serverSecure
    events:
      - http:
          path: server/secure
          method: post
          cors: true

  playground:
    handler: handler.playground
    events:
      - http:
          path: playground/public
          method: get
          cors: true
  playgroundSecure:
    handler: handler.playgroundSecure
    events:
      - http:
          path: playground/secure
          method: get
          cors: true
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.