Graphql - I can not get fragments to work

I am new to Graphql so I dont know if it is on the server side og client side when this happen - I can not get fragments to work:

fragment NameParts on Author{
  id
}

query getAuthor{
  getAuthor(id: "0x36"){
    ...NameParts
  }
}

@JCzz Fragments are currently not supported. It’s still early days for the GraphQL API for Dgraph, so that query should work eventually. But for now you can specify the fields within the query block itself without fragments.

Thanks, quick question - Just want to make sure, is “input objects” also not yet implemented?

input AuthorAndComment {
  author: Author
  post: Post
}

“enum, interface, scalar, implements” work fine. The input I think is auto-generated by Dgraph layer. Due to some dealing with the DB. I’m not sure if you can use, but it is supported.

1 Like

@JCzz That’s right, inputs are auto generated from your types - so for each type you get auto generated input objects, filters for search, queries and mutations.

You can’t add your own input objects.

2 Likes

@michaelcompton
@MichelDiz
Thanks guys

Respect dgraph is still in early days regarding graphql - and there is also a good chance that I am missing something, still new(operative) to dgraph and also new to graphql.

"Author" with “Comments” in one post
That said; what if I wanted to add/delete an “Author” with “Comments” in one post(Author from example graphql.dgraph.io). This is where I found that “input object” can be used, how would you do it - or if at all possible using graphql, now that “input objects” is nicely auto generated for you?

Just for reference - here I have the schema and last “AuthorAndComment” as input object:

type Author {
  id: ID!
  name: String! @search(by: [hash])
  posts: [Post] @hasInverse(field: author)
}

interface Post {
  id: ID!
  text: String @search(by: [fulltext])
  datePublished: DateTime @search
  author: Author! @hasInverse(field: posts)
}

type Question implements Post {
  answers: [Answer]
}

type Answer implements Post {
  inAnswerTo: Question!
}

type Comment implements Post {
  commentsOn: Post!
}

input AuthorAndComment {
  author: Author
  post: Post
}

Hi,

Thanks for looking deeply and for all the questions.

I think I understand that you want to make one mutation that adds a new author and a comment/question for that author in a single mutation. Is that right?

You can currently do that just fine in multiple mutations (add the author, then add the comment). But we are also adding deeper mutations (I’m writing it right now … well I took a break to answer your question :slight_smile: ).

(sorry these following examples might not be 100% in line with your schema, but I hope you get the idea.)

So you’ll be able to write add mutations like:

mutation {
    addAuthor(input: {
        name: "JCzz",
        questions: [ 
            {
                text: "Hey how do I add an author and a list of posts in one go"
                datePublished: ...
            }
        ]
    })
}

That’ll add the new author and a list of questions for them in a single mutation.

You’ll also be able to do the same in an update. E.g. update a Question to add a new comment and an author with that comment, like this:

mutation {
    updateQuestion(input: {
        filter: { ids: [ "0x123" ]},
        set: {
            comments: [
                {
                    text: "a new comment"
                    author: { name: "My New Author", ... }
                }
            ]
        }
    })
}

We are working out to add a new author or link with an existing based on what you pass in. E.G. the following already has an id, so we know it’s link this new comment to an existing author:

mutation {
    updateQuestion(input: {
        filter: { ids: [ "0x123" ]},
        set: {
            comments: [
                {
                    text: "a new comment"
                    author: { id: "0x456" }
                }
            ]
        }
    })
}

Hi @michaelcompton

Thank you very much for you answer. That was exactly what I was looking for - I like that you used "..name: "JCzz",.." in you example :slight_smile:

Sorry, but I can not make it work. I get the followin from reading the schema - using Altait GraphQL Client:

addAuthor ( input AuthorInput! ) AddAuthorPayload
  
AuthorInput:
name String!
posts [PostRef]

I change your mutation to using posts and not questions - I tought it might have to do with the type “interface Post”:

mutation {
    addAuthor(input: {
        name: "JCzz",
        posts: [ 
            {
                text: "Hey how do I add an author and a list of posts in one go"
                datePublished: "2020-10-02T10:00:00-05:00"
            }
        ]
    })
}

This error message when trying to post:


{
  "errors": [
    {
      "message": "Field PostRef.id of required type ID! was not provided.",
      "locations": [
        {
          "line": 5,
          "column": 13
        }
      ]
    },
    {
      "message": "Field \"text\" is not defined by type PostRef.",
      "locations": [
        {
          "line": 6,
          "column": 17
        }
      ]
    },
    {
      "message": "Field \"datePublished\" is not defined by type PostRef.",
      "locations": [
        {
          "line": 7,
          "column": 17
        }
      ]
    },
    {
      "message": "Field \"addAuthor\" of type \"AddAuthorPayload\" must have a selection of subfields. Did you mean \"addAuthor { ... }\"?",
      "locations": [
        {
          "line": 2,
          "column": 5
        }
      ]
    }
  ],
  "extensions": {
    "requestID": "752579d5-b135-42d8-bad6-4194267cf29a"
  }
}

I can’t wait to see deep mutation very cool :wink: Keep up the good work and sorry for disturbing you - but I really want to use dgraph

Thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.