代码之家  ›  专栏  ›  技术社区  ›  left click

如何解决GraphQL中的命名冲突?

  •  1
  • left click  · 技术社区  · 6 年前
    import { makeExecutableSchema } from 'graphql-tools';
    
    const fish = { length:50 },
          rope = { length:100 };
    
    const typeDefs = `
        type Query {
            rope: Rope!
            fish: Fish!
        }
    
        type Mutation {
            increase_fish_length: Fish!
            increase_rope_length: Rope!
        }
    
        type Rope {
            length: Int!
        }
    
        type Fish {
            length: Int!
        }
    `;
    
    const resolvers = {
        Mutation: {
            increase_fish_length: (root, args, context) => {
                fish.length++;
                return fish;
            },
            increase_rope_length: (root, args, context) => {
                rope.length++;
                return rope;
            }
        }
    };
    
    export const schema = makeExecutableSchema({ typeDefs, resolvers });
    

    增加长度 而不是 增加绳索长度 .

    鱼/增加长度 绳索/增加长度

    GraphQl是否支持命名空间的任何解决方案?

    2 回复  |  直到 6 年前
        1
  •  1
  •   yussenn    6 年前

    Graphql不支持类似名称空间的东西

        2
  •  1
  •   Dan Crews    6 年前

    我一直在玩弄关于名称空间的一些想法。如果您的类型定义看起来像这样:

    type Mutation {
        increase_length: IncreaseLengthMutation!
    }
    
    type IncreaseLengthMutation {
        fish: Fish!
        rope: Rope!
    }
    

    你的分解器是这样的:

    const resolvers = {
        Mutation: {
            increase_Length: () => {
                return {}
            }
        },
        IncreaseLengthMutation {
            fish: (root, args, context) => {
                fish.length++;
                return fish;
            },
            rope: (root, args, context) => {
                rope.length++;
                return rope;
            }
        }
    };