0

Say I have this:

type Page {
  id: ID!
  title: String!
  body: String!
  comments: [Comment]
}

type Comment {
   id: ID!
   text: String!   
   someOtherField: String!
   optional: String
}

mutation AddPage (input: AddPageInput): Page!

input AddPageInput {
  title: String!
  body: String
  comment: AddPageCommentInput
}

input AddPageCommentInput {
   text: String!   
   someOtherField: String!
   optional: String
}

mutation AddComment (input: AddCommentInput): Comment!

input AddCommentInput {
   forPageId: ID!
   text: String!   
   someOtherField: String!
   optional: String
}

I would like to deduplicate that AddPageCommentInput as it has the same fields as AddCommentInput except forPageId.

What I'm trying to do is to get the AddPage resolver to just delegate the AddPageCommentInput to AddComment resolver (after saving the Page and extending the input with forPageId)

Is the inheritance (with manually typing the definitions) the only way to make sure things are consistent ?


edit I don't have these types, it is just an attempt to provide context for the problem.

A better worded question is : "what options do I have to mimic inheritance for input ?"

the-noob
  • 1,322
  • 2
  • 14
  • 19
  • Possible duplicate of [How to share common fields btw Input and Type in GraphQL](https://stackoverflow.com/questions/52101396/how-to-share-common-fields-btw-input-and-type-in-graphql) – Dan Dascalescu May 19 '19 at 04:42
  • 'duplicate' of a question asked 3 months after mine... indeed, indeed – the-noob May 20 '19 at 15:28
  • Question age is not the deciding factor when choosing a duplicate. Please see [this discussion on meta.SE](https://meta.stackoverflow.com/questions/315472/old-question-marked-as-duplicate-of-a-new-question). – Dan Dascalescu May 20 '19 at 20:05

2 Answers2

0

You can subvert the problem by remodelling the mutation a bit (and making it clearer in the process):

input CommentInput {
   text: String!   
   someOtherField: String!
   optional: String
}

input AddPageInput {
  title: String!
  body: String
  comment: CommentInput
}

input AddCommentToPageInput {
   pageId: ID!
   comment: CommentInput!
}

type AddCommentToPagePayload {
    page: Page!
    comment: Comment!
}

mutation AddPage(input: AddPageInput!): Page!
mutation AddCommentToPage(input: AddCommentToPageInput!): AddCommentToPagePayload!
Andrew Ingram
  • 5,160
  • 2
  • 25
  • 37
0

Here's a suggestion: you can take out the forPageId field out from AddCommentInput and use it as a second argument. That will let you discard AddPageCommentInput and use AddCommentInput in both places.

The code will look something like this:

mutation AddPage (input: AddPageInput): Page!

input AddPageInput {
  title: String!
  body: String
  comment: AddCommentInput
}

mutation AddComment (forPageId: ID!, input: AddCommentInput): Comment!

input AddCommentInput {
   text: String!   
   someOtherField: String!
   optional: String
}
Tal Z
  • 3,170
  • 17
  • 30