简体   繁体   English

如何解决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 });

Above example is working well, but I want to use a mutation name increase_length instead of increase_fish_length and increase_rope_length . 上面的例子是运作良好,但我想用一个突变的名字increase_length代替increase_fish_lengthincrease_rope_length。

I tried to name mutations Fish/increase_length and Rope/increase_length using slash, but it didn't work. 我尝试使用斜杠将突变Fish / increase_lengthRope / increase_length命名,但是没有用。 (only /[_A-Za-z][_0-9A-Za-z]*/ are available.) (仅/ [_ A-Za-z] [_ 0-9A-Za-z] * /可用。)

Dose GraphQl support any solution for namespace? GraphQl是否支持任何名称空间解决方案?

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

I've been toying around with some ideas around namespaces. 我一直在研究关于命名空间的一些想法。 What if your typeDefinitions looked something like this: 如果您的typeDefinitions看起来像这样:

type Mutation {
    increase_length: IncreaseLengthMutation!
}

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

and your resolvers looked like this: 您的解析器如下所示:

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

The biggest drawback is the wonky mutation resolver that returns an empty array. 最大的缺点是该突变的解析器返回一个空数组。 It has to exist so that it cascades down to the other mutations, though. 它必须存在,以便可以级联到其他突变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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