0

In the following example we have a mutation that create a post. This is a simple example with only 2 params, title and url. What if this post has a lot of arguments. Is there any other way to pass the params? Or only one by one. Can we pass o whole object?

newPost() {
  this.apollo.mutate({
  mutation: gql`mutation($title: String, $url: String) {
    createPost(title: $title, url: $url) {
      title
      url
    }
  }
 `,
 variables: {
   title: this.someForm.get('title'),
   url: this.someForm.get('url')'
 }
}).subscribe(data => {
   console.log('New post created!', data);
});
}

1 Answer 1

1

This is something that's specific to the server-side implementation. For example, the server's schema could look something like this:

type Mutation {
  createPost(title: String! url: String!): Post
}

In this case, if you're using variables, as a client you're forced to use one variable per argument since GraphQL does not support some kind of "spread" syntax. However, the schema can utilize an Input Object Type to group a number of arguments, like this:

type Mutation {
  createPost(input: CreatePostInput!): Post
}

input CreatePostInput {
  title: String!
  url: String!
}

In this case, you can use a single variable that's an object:

mutation($input: CreatePostInput!) {
  createPost(input: $input) {
    title
    url
  }
}

But, again, the schema has to support this sort of usage. As a client, you do not have control over that.

Sign up to request clarification or add additional context in comments.

2 Comments

Do I have to set type CreatePostInput to the angular? I have only set it on the backend schema. But even it works.... webstorm says "Unknown type CreatePostInput"
Hard to say where that warning's coming from without more details. Since that's unrelated to your original question, if you can't solve it, consider searching SO and if there isn't already a question you can post a new one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.