So erstellen Sie eine Mutationsabfrage zum Einfügen einer Liste von (Array-) Feldern in GraphQL
vor kurzem begann ich, GraphQL, ich bin in der Lage, das einfügen von Daten in flat-schema, ohne jedes problem, aber wenn es um ein Array von Daten, die ich erhalte eine Fehlermeldung wie
{ "errors": [ { "message": "Must be input type" } ]}
Teste ich meine Abfrage mit Postboten, meine mutation Abfrage
mutation M {
AddEvent
(
title: "Birthday event"
description:"Welcome to all"
media:[{url:"www.google.com", mediaType:"image" }]
location:[{address:{state:"***", city:"****"}}]
)
{title,description,media,location,created,_id}}
Dies ist meine Veranstaltung-Schema:
EventType = new GraphQLObjectType({
name: 'Event',
description: 'A Event',
fields: () => ({
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
id: {
type: GraphQLString,
description: 'The id of the event.',
},
title: {
type: GraphQLString,
description: 'The title of the event.',
},
description: {
type: GraphQLString,
description: 'The description of the event.',
},
media:{
type:new GraphQLList(mediaType),
description:'List of media',
},
location:{
type:new GraphQLList(locationType),
description:'List of location',
}
})
});
//Media Type
export var mediaType = new GraphQLObjectType({
name: 'Media',
description: 'A Media',
fields: () => ({
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
url:{
type: GraphQLString,
description: 'The url of the event.',
},
mediaType:{
type: GraphQLString,
description: 'The mediaTypa of the event.',
}
})
});
//Location Type
export var locationType = new GraphQLObjectType({
name: 'Location',
description: 'A location',
fields: () => ({
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
address:{
type: GraphQLString,
description: 'The address.',
},
state:{
type: GraphQLString,
description: 'The state.',
},
city:{
type: GraphQLString,
description: 'The city.',
},
zip:{
type: GraphQLString,
description: 'The zip code.',
},
country:{
type: GraphQLString,
description: 'The country.',
}
})
});
Mungo Schema:
var EventSchema = new mongoose.Schema({
title: {
required: true,
type: String,
trim: true,
match: /^([\w ,.!?]{1,100})$/
},
description: {
required: false,
type: String,
trim: true,
match: /^([\w ,.!?]{1,100})$/
},
media: [{
url: {
type: String,
trim: true
},
mediaType: {
type: String,
trim: true
}
}],
location: [{
address: {
type: String
},
city: {
type: String
},
state: {
type: String
},
zip: {
type: String
},
country: {
type: String
}
}]
})
Mutation Typ:
addEvent: {
type: EventType,
args: {
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
title: {
type: GraphQLString,
description: 'The title of the event.',
},
description: {
type: GraphQLString,
description: 'The description of the event.',
},
media:{
type:new GraphQLList(mediaType),
description:'List of media',
},
location:{
type:new GraphQLList(locationType),
description:'List of media',
},
created: {
type: GraphQLInt,
description: 'The created of the user.',
}
},
resolve: (obj, {title,description,media,location,created,_id}) => {
let toCreateEvent = {
title,
description,
created:new Date(),
start: new Date(),
media,
location,
_id,
};
return mongo()
.then(db => {
return new Promise(
function(resolve,reject){
let collection = db.collection('events');
collection.insert(toCreateEvent, (err, result) => {
db.close();
if (err) {
reject(err);
return;
}
resolve(result);
});
})
});
}
}
InformationsquelleAutor der Frage Mahesh | 2015-08-31
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ihre Frage ist, dass, wenn Sie definieren, Mutationen aller Art müssen sein input-Typen, daher die Fehlermeldung, die Sie erhalten
"Must be input type"
. Also hier (von deiner mutation):GraphQLList
mediaType
undlocationType
werden muss-input-Typen.GraphQLList
schon einen input geben (siehe hier https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82um die Liste der GraphQL Typen als input-Typen).Jedoch Ihre Typen
mediaType
undlocationType
sindGraphQLObjectType
Art, die nicht einer input type aber wenn man sich die Liste der Eingabetypen wieder: https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82finden SieGraphQLInputObjectType
was ist ein Objekt, input type, so, was Sie tun müssen, ist zu ersetzenmediaType
undlocationType
durch Ihren "input" - version.Was ich vorschlagen, zu tun ist, zu erstellen
mediaInputType
undlocationInputType
hätten Sie das gleiche Feld Struktur alsmediaType
undlocationType
aber erstellt mitnew GraphQLInputObjectType({...
stattnew GraphQLObjectType({...
und verwenden Sie Sie in Ihrer mutation.Ich lief in das gleiche Problem, und ich löste es so, fühlen Sie sich frei zu kommentieren, wenn Sie irgendwelche Fragen haben.
InformationsquelleAutor der Antwort adriantoine
Ich lief in das gleiche problem - ich wusste nicht, wie Sie angeben, array von Objekten in der input-definition. Also für diejenigen, die will, um zu sehen, eine "text" - schema Lösung:
haben ein array von Büchern in Ihrem input type
können Sie nicht nur
books: [Book!]
innerhalb der input-Anweisung, müssen Sie bewusst erzeugen input type enthält benötigte Felder (doppelt, wenn du magst):dann können Sie:
InformationsquelleAutor der Antwort bora89