简体   繁体   English

在 GraphQL 模式中创建类型时是否可以重命名字段?

[英]Is it possible to rename a field when creating a type within a GraphQL schema?

When defining the userType in the following GraphQL schema on the server, how can I rename the "name" field to "firstname" while still referring to the "name" field in fakeDatabase ?在服务器上的以下 GraphQL 模式中定义userType时,如何将“name”字段重命名为“firstname”,同时仍然引用fakeDatabase的“name”字段?

The following code snippet has been copied from the official GraphQL docs以下代码片段已从官方 GraphQL 文档中复制

var express = require('express');
var graphqlHTTP = require('express-graphql');
var graphql = require('graphql');

// Maps id to User object
var fakeDatabase = {
  'a': {
    id: 'a',
    name: 'alice',
  },
  'b': {
    id: 'b',
    name: 'bob',
  },
};

// Define the User type
var userType = new graphql.GraphQLObjectType({
  name: 'User',
  fields: {
    id: { type: graphql.GraphQLString },
    // How can I change the name of this field to "firstname" while still referencing "name" in our database?
    name: { type: graphql.GraphQLString },
  }
});

// Define the Query type
var queryType = new graphql.GraphQLObjectType({
  name: 'Query',
  fields: {
    user: {
      type: userType,
      // `args` describes the arguments that the `user` query accepts
      args: {
        id: { type: graphql.GraphQLString }
      },
      resolve: function (_, {id}) {
        return fakeDatabase[id];
      }
    }
  }
});

var schema = new graphql.GraphQLSchema({query: queryType});

var app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');

Resolvers can be used for any type, not just Query and Mutation .解析器可用于任何类型,而不仅仅是QueryMutation That means you can easily do something like this:这意味着您可以轻松地执行以下操作:

const userType = new graphql.GraphQLObjectType({
  name: 'User',
  fields: {
    id: {
      type: graphql.GraphQLString,
    },
    firstName: {
      type: graphql.GraphQLString,
      resolve: (user, args, ctx) => user.name
    },
  }
})

The resolver function specifies, given the parent value, arguments for that field and the context, what a field for any instance of a type will resolve to.解析器函数指定,给定父值,该字段的参数和上下文,类型的任何实例的字段将解析为什么。 It could even always return the same static value each time.它甚至可以每次总是返回相同的静态值。

There's also a library graphql-tools that let you transform your schema, we use that on our schema stitching service.还有一个库graphql-tools可以让您转换您的架构,我们在我们的架构拼接服务中使用它。

const { RenameTypes, transformSchema } = require("graphql-tools");

/*
 * Schema transformations:
 * Types:
 *  <> Task -> GetTask
 */

const transformMySchema = schema => {
  return transformSchema(schema, [
    new RenameTypes(function(name) {
      return name == "Task" ? "GetTask" : name;
    }),
  ]);
};

Read more : https://github.com/apollographql/graphql-tools/blob/513108b1a6928730e347191527cba07d68aadb74/docs/source/schema-transforms.md#modifying-types阅读更多: https : //github.com/apollographql/graphql-tools/blob/513108b1a6928730e347191527cba07d68aadb74/docs/source/schema-transforms.md#modifying-types

Does this answer the question ?这是否回答了问题?

Exactly what Daniel said. 正是丹尼尔所说的。 It does not matter what your resolver name is, you will resolve your user.name from your DB. 解析程序名称是什么都无所谓,您将从数据库中解析user.name

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

相关问题 GraphQL 模式 - 是否可以在自定义类型字段上设置约束 - GraphQL schema - is it possible to set constraints on a custom type field 数组中类型为graphql的对象的GraphQL字段 - GraphQL field of type graphql object within an array 使用 Hasura graphql 模式时如何自定义 graphql-code-generator 生成字段的类型 - How to customize the type for a graphql-code-generator produced field when consuming a Hasura graphql schema GraphQL-有条件地确定架构中字段的类型 - GraphQL- conditionally determine the type of a field in a schema Graphql:完全更改架构中的字段类型 - Graphql: completely change field type in schema 如何在 GraphQL 模式中使用“十进制”作为字段类型? - How to use 'Decimal' as a field type in GraphQL schema? 定义GraphQL模式时,获取字段类型必须为Output Type错误 - Getting field type must be Output Type error when defining a GraphQL schema 如何重命名从 Django 模型自动生成的 GraphQL 类型字段? - How to rename GraphQL type field autogenerated from Django model? Graphql 模式类型 - Graphql Schema Type GraphQL / Relay架构无法在“CreateLinkPayload”类型上查询字段“store” - GraphQL/Relay Schema Cannot query field “store” on type “CreateLinkPayload”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM