【问题标题】:.gql or .graphql file types with Node带有 Node 的 .gql 或 .graphql 文件类型
【发布时间】:2017-09-21 12:34:10
【问题描述】:

我将 Apollo 的 graphql-server-express 与 Node 一起使用,并且我想将我的“typedef”模式定义文件转换为 .graphql or .gql 文件以清晰和突出语法。

最好的方法是什么?除了 Babel(?) 之外,我在网上找不到任何好的资源,这似乎是唯一的选择。

非常感谢您的帮助!

【问题讨论】:

  • 您可以拥有一个包含所有.gql 文件的目录。通过读取文件将其加载到 Node 中。

标签: node.js express graphql babeljs


【解决方案1】:

架构文件夹应该包含带有 .graphql 或 .gql 文件的文件

const path = require('path');
const { loadFilesSync } = require('@graphql-tools/load-files');
const { mergeTypeDefs } = require('@graphql-tools/merge');

const typesArray = loadFilesSync(path.join(__dirname, './schemas'));

module.exports = mergeTypeDefs(typesArray, { all: true });

【讨论】:

    【解决方案2】:

    我在使用 express-graphql 和 TypeScript 时的解决方案:

    import {buildSchema} from 'graphql';
    import graphqlHTTP from 'express-graphql';
    import {importSchema} from 'graphql-import';
    
    import {resolvers} from './graphql/resolvers';
    
    const server = express();
    
    server.use(
      '/graphql',
      graphqlHTTP({
        schema: buildSchema(importSchema('**/*.graphql')),
        rootValue: resolvers,
      }),
    );
    

    【讨论】:

      【解决方案3】:

      有一个相同的问题,如何以正确的方式实现它。无论如何,这对我有用。

      import {gql} from 'apollo-server-express'
      import * as types from './types.graphql'
      
      export const typeDefs = gql`${types}`
      

      【讨论】:

        【解决方案4】:

        我确定您到目前为止找到了解决方案,但对其他人来说这可能会有所帮助 https://github.com/prismagraphql/graphql-import

        用法

        import { importSchema } from 'graphql-import'
        import { makeExecutableSchema } from 'graphql-tools'
        
        const typeDefs = importSchema('schema.graphql')
        const resolvers = {}
        
        const schema = makeExecutableSchema({ typeDefs, resolvers })
        

        示例

        导入特定字段

        假设如下目录结构:

        .
        ├── a.graphql
        ├── b.graphql
        └── c.graphql
        

        a.graphql

        # import B from "b.graphql"
        
        type A {
          # test 1
          first: String
          second: Float
          b: B
        }
        

        b.graphql

        # import C from 'c.graphql'
        
        type B {
          c: C
          hello: String!
        }
        

        c.graphql

        type C {
          id: ID!
        }
        

        【讨论】:

        • 欢迎提供解决方案链接,但请确保您的答案在没有它的情况下有用:add context around the link 这样您的其他用户就会知道它是什么以及为什么会出现,然后引用最相关的您链接到的页面的一部分,以防目标页面不可用。可能会删除仅是链接的答案。
        • 包好像停产了
        【解决方案5】:

        我有同样的问题,我相信在不将 babel/webpack 引入服务器端管道的情况下完成您所要求的唯一方法是将您的查询编写为导出的模板字符串。

        例如

        /* query.gql */
        module.exports = `
        schema {
          query: Query
          mutation: Mutation
        }
        
        type Query {
          posts: [Post]
          post(id:String, slug:String): Post
          user(uid:String): User
        }
        ...
        `
        

        谢天谢地,GraphQL 的 VS Code 语法高亮支持此方法。

        【讨论】:

          猜你喜欢
          • 2018-05-01
          • 2020-10-13
          • 2021-04-13
          • 2020-12-09
          • 2019-04-03
          • 2019-05-13
          • 2018-03-05
          • 2020-05-19
          • 1970-01-01
          相关资源
          最近更新 更多