[英]GraphQL mutation on id field
I have a GraphQL schema called Car Manufacturer which has two fields, id
and name
. 我有一个名为Car Manufacturer的GraphQL模式,其中有两个字段,
id
和name
。 What I am trying to accomplish via GraphiQL is to insert several data into the schema but I need to mutate both the mentioned fields. 我试图通过GraphiQL完成的工作是在模式中插入几个数据,但是我需要对两个上述字段进行突变。
Is there a way to insert a desired value on the id
field? 有没有一种方法可以在
id
字段中插入所需的值?
Why you want to modify the id
? 为什么要修改
id
? For using them as indexes? 使用它们作为索引? Like
id: 1
, id: 2
an so on? 像
id: 1
, id: 2
等等? If is that so, you should have something like globalIdField
from graphql-relay
in my opinion. 如果是这样,我认为您应该从
graphql-relay
中获得诸如globalIdField
东西。 Not mutate these ids
yourself. 不要自己更改这些
ids
。 For example: 例如:
import { GraphQLObjectType, GraphQLFloat } from 'graphql';
import { globalIdField } from 'graphql-relay';
export default new GraphQLObjectType({
name: 'Car anufacturer',
description: 'desc',
fields: () => ({
id: globalIdField('CarManufacturer'),
title: {
type: GraphQLString,
resolve: async obj => obj.title,
},
}),
});
This way you will have unique ids and you can turn these ids on an ObjectId
for use with MongoDB
for example. 这样,您将拥有唯一的ID,例如,您可以将这些ID转换为与
MongoDB
一起使用的ObjectId
。
If you want to change this values for whatever other reason, you have to create Mutations
just like @arian said 如果您出于任何其他原因想要更改此值,则必须像@arian一样创建
Mutations
I hope that I helped you someway :) 希望我能对您有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.