简体   繁体   中英

How to import, export, and merge several GraphQL type definition in NodeJS

The file structure is like this:

.
├── typeDefs.js
├── server.js
└── types
     ├── types_index.js
     └── addressResolver
          ├── addressResolver_index.js
          ├── address.js
          ├── Mutation.js 
          └── Query.js

in ./types/addressResolver , I saved GraphQL type definition files as the following:

  • In ./types/addressResolver/address.js

     export const Address = gql` type Address { id: ID,: customer_id, ID:, address_type: String!, address: String! }

    `

  • In ./types/addressResolver/Mutation.js

     export const addressMutation = gql` type Mutation { "Add a new address." addAddress(userId: String,: addressType, String:: address. String:), Boolean: "Delete an address," deleteAddress(userId: String:, addressType: String!, address: String!): Boolean! }

    `

  • In ./types/addressResolver/Query.js

     export const addressQuery = gql` type Query { resolveAddress(userId: String,: addressType: String!): Address! }

    `

I would like to pass these three gql from ./types/addressResolver/addressResolver_index.js >> ./types/types_index.js >> ./typeDefs.js

So eventually, I would like to have something like the following in ./typeDefs.js :

export const typeDefs = gql`
type Query{
    hello: String!
    cats:[Cat!]!
    resolveAddress(userId: String!, addressType: String!): Address!
    address:[Address!]!
}

type Address {
    id: ID!,
    customer_id: String!,
    address_type: String!,
    address: String!
}

type Cat{
    id: ID!
    name: String!
}

type Mutation{
    createCat(name: String!): Cat!

    "Add a new address."
    addAddress(userId: String!, addressType: String!, address: String!): Boolean!

    "Delete an address."
    deleteAddress(userId: String!, addressType: String!, address: String!): Boolean!
}
`

What I'm doing right now is:

  • In ./types/addressResolver/addressResolver_index.js :

     export {Address} from './address.js'; export {addressMutation} from './Mutation.js'; export {addressQuery} from './Query.js';
  • In ./types/types_index.js :

     export {Address, addressMutation, addressQuery} from './addressResolver/index.js';
  • In ./typeDefs.js :

     import {Address, addressMutation, addressQuery} from './types/index.js'; export const typeDefs = [Address, addressMutation, addressQuery]

But it does not work.

PS I'm using ES6, so I have to stick to export instead of module.exports = , and import instead of require

I found the solution by using the following code in the ./types/addressResolver/addressResolver_index.js

import { mergeTypeDefs} from '@graphql-tools/merge';
import {Address} from './address.js';
import {addressMutation} from './Mutation.js';
import {addressQuery} from './Query.js';

const toMergeTypes = [Address, addressMutation, addressQuery];
const mergedTypes = mergeTypeDefs(toMergeTypes);
export const addressResolverTypes = mergedTypes;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM