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

How to solve naming conflict in GraphQL?

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_lengthincrease_rope_length.

我尝试使用斜杠命名突变 Fish/increase_lengthRope/increase_length,但没有成功. (只有 /[_A-Za-z][_0-9A-Za-z]*/ 可用。)

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

Graphql 不支持命名空间

我一直在思考关于名称空间的一些想法。如果您的 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;
        }
    }
};

最大的缺点是不稳定的变异解析器 returns 一个空数组。不过,它必须存在才能级联到其他突变。