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

时间:2018-08-18 13:40:53

标签: javascript graphql graphql-tools

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 });

上面的示例运行良好,但我想使用突变名称​​ increase_length 代替 increase_fish_length increase_rope_length

我尝试使用斜杠为 Fish / increase_length Rope / increase_length 命名突变,但是没有成功。 (仅/ [_ A-Za-z] [_ 0-9A-Za-z] * /可用。)

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我一直在研究关于名称空间的一些想法。如果您的typeDefinitions看起来像这样:

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;
        }
    }
};

最大的缺点是该突变的解析器返回一个空数组。它必须存在,以便可以级联到其他突变。