I am trying to fetch a user from graphql playground which has both queries , when i hit the fetch button and select GetUser it still send send both query as string to server.
My question is how to extract all data / fragments / query on backend related to one operation in this case it's GetUser because this query is sent to other service from backend which doesn't have CreateUser implementation and query fails. I am currently using NestJs
mutation CreateUser ($input: CreateUserInput) {
createUser (input: $input) {
... on User {
id
}
}
}
query GetUser ($input: GetUserInput) {
getUser (input: $input) {
... on User {
id
}
}
}
Related
I have a GraphQL mutation that creates an item and returns the data that's created. I need to pass some of the created fields into a query. Is this possible? This is almost working but I can't figure out how to get the data between the mutation and the query:
mutation {
createToken(
input: { tokenname: "my token", tokendescription: "my valuable token" }
) {
id
randomdata
}
insert__helloworld_article(objects: [{randomdata: "Hello" , author_id: 1}]) {
returning {
id
}
}
}
So my problem is getting "randomdata" from the mutation to insert into the helloworld_article
You wouldn't be able to use the return value in the same mutation with GraphQL, however if those objects have a relationship you could do a nested insert.
I am performing a request for an individual post from Apollo Server / Express backend.
In the Apollo GraphQL sandbox, the query works and retrieves the correct post, however, the query has a red squiggle identifying an error which reads -
Variable "$getPostId" is never used in operation "Query".
The query is as follows -
query Query($getPostId: ID!) {
getPost(id:"20c9b3ac-afe6-4faa-a3f9-e00ef1b38ccf") {
title
author
id
}
}
The schema is as follows -
module.exports = gql`
type Post {
id: ID!
title: String!
author: String!
}
type Query {
getPosts: [Post]!
getPost(id: ID!): Post
}
...
`
The closest post which seems to address a similar problem I could find is here. However, I can't translate the resolution to my problem.
Why is the error showing (particularly when the query runs successfully)? What needs to be done to stop the error from showing?
Many thanks!
It sounds like
query Query($getPostId: ID!) {
getPost(id:"20c9b3ac-afe6-4faa-a3f9-e00ef1b38ccf") {
title
author
id
}
}
is supposed to be
query Query($getPostId: ID!) {
getPost(id: $getPostId) {
title
author
id
}
}
Or if your query is actually meant to hard-code the ID, then you want
query Query {
getPost(id:"20c9b3ac-afe6-4faa-a3f9-e00ef1b38ccf") {
title
author
id
}
}
I'm trying to create a mutation that calls a child resolver in addition to the parent resolver if an optional parameter is sent in.
I'm using AWS AppSync to sent my queries to Lambda. AppSync creates and sends an AppSyncEvent to my resolver file that looks something like this:
{
"info": {
"parentTypeName": "Mutation",
"selectionSetList": [
...
],
"selectionSetGraphQL": "...",
"fieldName": "updateUser",
"variables": {}
}
}
This event gets passed to my lambda function where, based on the fieldName and parentTypeName, I call my updateUser function.
I have the below schema
schema {
query: Query
mutation: Mutation
}
type Query {
getUser(id: ID!): User
}
type Mutation {
updateUser(name: String, email: String, bookRead: BookReadInput): User
}
type User {
name: String
email: String
booksRead: [Book]
}
type Book {
title: String
author: String
}
type BookReadInput {
title: String
author: String
}
I want that if the mutation gets passed bookRead then it will know to call a child resolver called addBook besides for the regular updateUser resolver.
I've seen various articles about implementing child resolvers but I can't figure out how they can work with lambda and the way my resolvers work.
The lambda could inspect the selectionSetList and decide what to do with the BookReadInput fields.
See https://aws.amazon.com/blogs/mobile/appsync-and-the-graphql-info-object/
You could also go with pipeline resolvers to first update the user, and then add the book.
I don't think there is a way to have it automated. You need to set it up, one way or the other.
I cannot find some example of a schema where the mutations are nested in it's own resolver, to group some actions in a sub-mutations object.
Something like this:
schema {
query: Query
mutation: RootMutation
}
type RootMutation {
user: UserMutation
post: PostMutation
}
type UserMutation {
create(user: UserInput!): User
delete(id: ID!): Bool
}
type PostMutation {
create(user: PostInput!): Post
delete(id: ID!): Bool
}
And the query would be like this:
mutation CreateUser($u: UserInput!) {
user {
create($u) {
id
}
}
}
I think that, this approach is completely ok with the specification. So why does everybody design the mutations as a first class function? Like so:
type RootMutation {
createUser(user: UserInput!): User
deleteUser(id: ID!): Bool
createPost(user: UserInput!): User
deletePost(id: ID!): Bool
}
When you got a lot of actions to make on your huge system, then the RootMutation will have just an endless list of similar functions (like create, delete and so on).
And isn't this a thing in REST? Where every function is it's own endpoint? But there it's better because you have the HTTP methods. So you would have an /user endpoint with a POST and a DELETE method and so on.
I'm wondering if there's a way in GraphiQL that will let me pipe the result from one query / mutation into another query / mutation. Here's an example where the login mutation is used to get a viewer and the viewer would be used to query the addresses for that user. Is this possible with GraphQL / GraphiQL.
mutation {
login(credentials: {
email: "me#me.com",
password: "password123",
passwordConfirmation: "password123"
}) {
viewer
}
}
query {
addresses(viewer:viewer) {
city
}
}
The purpose of the selection set in the mutations is to be able to fetch data that has changed as a result of the mutation. But it also makes it possible to fetch related data, as long as you can access is through the mutation result type.
Let's assume we have following types:
type Address {
city: String
}
type User {
addresses: [Address]
}
If the result (payload) type of the login mutation includes a field viewer of type User that refers to the successfully logged in user, you can query any field of the User in the result of the mutation:
mutation {
login(credentials: {
email: "me#me.com",
password: "password123",
passwordConfirmation: "password123"
}) {
viewer {
addresses {
city
}
}
}
}