1

I've been trying to make this one work. I am currently learning Apollo Server and GraphQL. I went to different documentations but I can't seem to find an answer. Here's what I am trying to do.

I have usertypes.js and here's the code snippet:

const { gql } = require('apollo-server');

const UserTypeDefs = gql`
  type User {
    _id: ID!,
    username: String!,
    password: String!,
  }
`;

module.exports = UserTypeDefs;

And on my logintypes.js:

const { gql } = require('apollo-server');
const { UserTypeDefs } = require('./usertypes.js') 

const LoginTypeDefs = gql`
    type LoginResponse {
        user: UserTypeDefs.User //this is not working. :(
    }

    extend type Mutation {
        login(username: String!, password: String!): LoginResponse!
    }
`;


module.exports = LoginTypeDefs;

Any help is appreciated. I don't want to declare my type User again in my logintypes.js as much as possible.

Thank you in advance.

ashlrem
  • 117
  • 1
  • 3
  • 17
  • 1
    Are you getting any errors? How do you define `not working`? Rather than redeclaring any types, as long as you create a single `executableSchema` composed of your `User` and `Login`, then there shouldn't be any errors as far as I understand. Here's an article to help understand how you can achieve this: https://blog.apollographql.com/modularizing-your-graphql-schema-code-d7f71d5ed5f2 – nopassport1 Feb 03 '20 at 15:35
  • I followed this link that you have provided and I was able to resolve my issue. I removed the `const { gql } = require('apollo-server');` from both of usertypes.js and logintypes.js then declare my typedefs as string instead of gql``; Thanks! – ashlrem Feb 04 '20 at 06:12
  • 1
    Happy to know you resolved it. Have fun – nopassport1 Feb 04 '20 at 08:39

1 Answers1

0

There's no need to import the other module. Just refer to the type by name:

type LoginResponse {
  user: User
}

Then, when you create your ApolloServer instance, pass in the typeDefs as an array:

const userTypeDefs = require(...)
const loginTypeDefs = require(...)

const apollo = new ApolloServer({
  typeDefs: [userTypeDefs, loginTypeDefs],
  ...
})
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183