2017-07-21 3 views
0

Ich bin neu mit GraphQL und ich versuche, eine Mutation zu tun, um einen Artikel aus meiner Datenbank zu löschen, aber ich kann nicht herausfinden, wie. Ich benutze Node.js, Mongoose und GraphQL.Was mache ich falsch mit dieser GraphQL-Abfrage?

Dies ist die Mutation in meinem Schema.

const Mutation = new GraphQLObjectType({ 
    name: 'Mutation', 
    description: 'Articles Mutations', 
    fields:() => ({ 
    remove: { 
     type: articleType, 
     description: 'Deletes an article by id', 
     args: { 
     id: { 
      type: new GraphQLNonNull(GraphQLString) 
     } 
     }, 
     resolve: (value, {id}) => { 
     return db.Article.findOneAndRemove({_id: new ObjectId(id)}); 
     } 
    } 
    }) 
}); 

Und das ist die Abfrage ich benutze, wenn die API-Aufruf einen Artikel zu löschen.

export const DELETE_ARTICLE_QUERY = (id) => (`{ 
    remove(id: "${id}") { 
    id 
    author 
    content 
    published 
    tags 
    title 
    excerpt 
    } 
}`); 

Was mache ich falsch?

Ich bekomme einen 400 Bad Request Fehler. Nachricht: "Feld kann nicht abgefragt werden" Entfernen "bei Typ" Mutation "."

+0

Wenn Sie eine Frage wie diese stellen, ist es hilfreich, so viele Details wie möglich anzugeben. Zum Beispiel: ob GraphQL oder Knoten beim Ausführen der Abfrage Fehler verursachen und was die Fehler sagen. –

Antwort

2

Wenn Sie eine GraphQL-Anforderung erstellen, ist es am besten, immer den Typ der Operation anzugeben, die Sie ausführen (Abfrage oder Mutation). Wenn Sie dies nicht tun, geht GraphQL davon aus, dass Sie eine Abfrage durchführen, was hier nicht der Fall ist. Sie haben nicht angegeben, ob Fehler aufgetreten sind, aber ich würde wetten, dass GraphQL etwas wie cannot query field remove on Query zurückgibt.

Ändern DELETE_ARTICLE_QUERY umfassen den Betrieb:

export const DELETE_ARTICLE_QUERY = (id) => (`mutation { 

Es ist eine gute Übung für Debugging-Zwecke eine Operation Namen, so könnte man sagen auch:

export const DELETE_ARTICLE_QUERY = (id) => (`mutation DeleteArticle { 

Edit: auf das Basierend Fehler, den Sie angegeben haben, es klingt wie das Schema-Objekt nicht richtig eingerichtet ist. Es sollte wie folgt aussehen:

const schema = new GraphQLSchema({ 
    query: new GraphQLObjectType({ 
    name: "WhateverNameYouWantForQuery", 
    fields: { 
     // each query is a property here 
    } 
    }), 
    mutation: new GraphQLObjectType({ 
    name: "WhateverNameYouWantForMutation", 
    fields: { 
     // each mutation is a property here 
    } 
    }), 
}); 

Wenn Sie Ihre Mutationen als separate Variable (Mutation) definieren, würden Sie den Wert der Mutation Eigenschaft als diese Variable unterstützen:

const schema = new GraphQLSchema({ 
    query: new GraphQLObjectType({ 
    // query props 
    }), 
    mutation: Mutation 
    }), 
}); 

Hier ist ein Arbeits Beispiel kannst du aus der Box heraus laufen und damit herumspielen. Nachdem Sie den Server gestartet haben, können Sie in Ihrem Browser unter http://localhost:3000/graphql auf die GraphiQL-Schnittstelle zugreifen und dort mögliche Abfragen/Mutationen ausprobieren.

const graphqlHTTP = require('express-graphql'); 
const app = require('express')(); 
const { 
    GraphQLSchema, 
    GraphQLObjectType, 
    GraphQLNonNull, 
    GraphQLString, 
    GraphQLList 
} = require('graphql'); 

const articleType = new GraphQLObjectType({ 
    name: 'Article', 
    fields: { 
    title: { 
     type: GraphQLString, 
    }, 
    }, 
}); 

const Mutation = new GraphQLObjectType({ 
    name: "RootMutationnnnn", 
    fields:() => ({ 
    remove: { 
     type: articleType, 
     args: { 
     id: { 
      type: new GraphQLNonNull(GraphQLString) 
     } 
     }, 
     resolve: (value, {id}) => { 
     return {title: 'Testing a delete'}; 
     } 
    } 
    }) 
}); 

const schema = new GraphQLSchema({ 
    query: new GraphQLObjectType({ 
    name: "RootQueryyyyy", 
    fields: { 
     articles: { 
     type: new GraphQLList(articleType), 
     resolve:() => { 
      return [{title: 'Test title'}, {title: 'Test title'}]; 
     } 
     } 
    } 
    }), 
    mutation: Mutation 
}); 

const root = {}; 

app.post('/graphql', graphqlHTTP({ 
    schema, 
    rootValue: root, 
    graphiql: false, 
})); 

app.get('/graphql', graphqlHTTP({ 
    schema, 
    rootValue: root, 
    graphiql: true, 
})); 

app.listen(3000, function(){ 
    console.log('listening on port 3000'); 
}); 
+0

Vielen Dank für Ihren Kommentar und sehr hilfreiche Tops. Allerdings bekomme ich immer noch 400 Bad Request Fehler :( –

+0

@JesusMendoza siehe meine Bearbeitung oben –