【问题标题】:How to solve naming conflict in GraphQL?如何解决 GraphQL 中的命名冲突?
【发布时间】:2019-01-25 07:35:46
【问题描述】:
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]*/ 可用。)

Dose GraphQl 支持命名空间的任何解决方案吗?

【问题讨论】:

    标签: javascript graphql graphql-tools


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我一直在玩弄关于命名空间的一些想法。如果你的 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;
              }
          }
      };
      

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

      【讨论】:

      • 是的~!我过去曾经将这个想法应用于Query。我得考虑一下。感谢您分享您的想法!
      猜你喜欢
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 2011-01-31
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      相关资源
      最近更新 更多