Entity Association syntax issue with graphql queries












0















I am trying to understand graphql's query and mutation syntax. Given this example:



type Author {
id: Int!
firstName: String
lastName: String
posts: [Post]
}

type Post {
id: Int!
title: String
author: Author
votes: Int
}

type Query {
posts: [Post]
author(id: Int!): Author
}


What should the queries look like to associate a post with the author? Is this where connections come into play or is that something else? Here is my attempt at trying to solve the problem but to avail.



mutation createAuthor {
createAuthor(input: {
id: 123
firstName: "Bob"
lastName: "Smith"
}) {
id
firstName
lastName
}
}

query listAuthors {
listAuthors {
items {
id
firstName
lastName
}
}
}

mutation createPost {
createPost(input: {
id: 12345
title: "Title"
votes: 345
author: {
lastName: {
contains: "Bob"
}
}
}) {
id
title
votes
author {
id
firstName
lastName
}
}
}


Any help on this would be appreciated. My Goal is to Query an author and return all of the post's associated with that author as well as create a Post Mutation that adds a post to an author.










share|improve this question





























    0















    I am trying to understand graphql's query and mutation syntax. Given this example:



    type Author {
    id: Int!
    firstName: String
    lastName: String
    posts: [Post]
    }

    type Post {
    id: Int!
    title: String
    author: Author
    votes: Int
    }

    type Query {
    posts: [Post]
    author(id: Int!): Author
    }


    What should the queries look like to associate a post with the author? Is this where connections come into play or is that something else? Here is my attempt at trying to solve the problem but to avail.



    mutation createAuthor {
    createAuthor(input: {
    id: 123
    firstName: "Bob"
    lastName: "Smith"
    }) {
    id
    firstName
    lastName
    }
    }

    query listAuthors {
    listAuthors {
    items {
    id
    firstName
    lastName
    }
    }
    }

    mutation createPost {
    createPost(input: {
    id: 12345
    title: "Title"
    votes: 345
    author: {
    lastName: {
    contains: "Bob"
    }
    }
    }) {
    id
    title
    votes
    author {
    id
    firstName
    lastName
    }
    }
    }


    Any help on this would be appreciated. My Goal is to Query an author and return all of the post's associated with that author as well as create a Post Mutation that adds a post to an author.










    share|improve this question



























      0












      0








      0








      I am trying to understand graphql's query and mutation syntax. Given this example:



      type Author {
      id: Int!
      firstName: String
      lastName: String
      posts: [Post]
      }

      type Post {
      id: Int!
      title: String
      author: Author
      votes: Int
      }

      type Query {
      posts: [Post]
      author(id: Int!): Author
      }


      What should the queries look like to associate a post with the author? Is this where connections come into play or is that something else? Here is my attempt at trying to solve the problem but to avail.



      mutation createAuthor {
      createAuthor(input: {
      id: 123
      firstName: "Bob"
      lastName: "Smith"
      }) {
      id
      firstName
      lastName
      }
      }

      query listAuthors {
      listAuthors {
      items {
      id
      firstName
      lastName
      }
      }
      }

      mutation createPost {
      createPost(input: {
      id: 12345
      title: "Title"
      votes: 345
      author: {
      lastName: {
      contains: "Bob"
      }
      }
      }) {
      id
      title
      votes
      author {
      id
      firstName
      lastName
      }
      }
      }


      Any help on this would be appreciated. My Goal is to Query an author and return all of the post's associated with that author as well as create a Post Mutation that adds a post to an author.










      share|improve this question
















      I am trying to understand graphql's query and mutation syntax. Given this example:



      type Author {
      id: Int!
      firstName: String
      lastName: String
      posts: [Post]
      }

      type Post {
      id: Int!
      title: String
      author: Author
      votes: Int
      }

      type Query {
      posts: [Post]
      author(id: Int!): Author
      }


      What should the queries look like to associate a post with the author? Is this where connections come into play or is that something else? Here is my attempt at trying to solve the problem but to avail.



      mutation createAuthor {
      createAuthor(input: {
      id: 123
      firstName: "Bob"
      lastName: "Smith"
      }) {
      id
      firstName
      lastName
      }
      }

      query listAuthors {
      listAuthors {
      items {
      id
      firstName
      lastName
      }
      }
      }

      mutation createPost {
      createPost(input: {
      id: 12345
      title: "Title"
      votes: 345
      author: {
      lastName: {
      contains: "Bob"
      }
      }
      }) {
      id
      title
      votes
      author {
      id
      firstName
      lastName
      }
      }
      }


      Any help on this would be appreciated. My Goal is to Query an author and return all of the post's associated with that author as well as create a Post Mutation that adds a post to an author.







      graphql aws-appsync






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 0:05







      Jghorton14

















      asked Nov 21 '18 at 23:41









      Jghorton14Jghorton14

      1591216




      1591216
























          1 Answer
          1






          active

          oldest

          votes


















          1














          There are 2 questions in one, so I'll answer in the order there were asked.



          1. return all of the posts associated with an author



          Your schema looks correct. The query would look like:



          query {
          author(id: 1) {
          id
          posts {
          id
          title
          }
          }
          }


          2. create Post and attach to an author



          In your example if you want to expose an interface to create a Post, then you would have to expose a mutation field in your schema



          e.g:



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


          if you want at the same time of creating a post, to also attach it to an author then you could add the authorId as part of the input, here we only want to attach the Post to an existing Author:



          input CreatePostInput {
          title: String
          authorId: ID!
          votes: Int
          }


          of course this is only the interface definition. We need to actually create the Post and link it to the Author inside the resolver.



          The mutation query will look like:



          mutation createPost {
          createPost(input: {
          title: "Title"
          votes: 345
          authorId: "authorId1"
          }) {
          id
          title
          votes
          author {
          id
          firstName
          lastName
          }
          }
          }


          Hope that helps!






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421982%2fentity-association-syntax-issue-with-graphql-queries%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            There are 2 questions in one, so I'll answer in the order there were asked.



            1. return all of the posts associated with an author



            Your schema looks correct. The query would look like:



            query {
            author(id: 1) {
            id
            posts {
            id
            title
            }
            }
            }


            2. create Post and attach to an author



            In your example if you want to expose an interface to create a Post, then you would have to expose a mutation field in your schema



            e.g:



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


            if you want at the same time of creating a post, to also attach it to an author then you could add the authorId as part of the input, here we only want to attach the Post to an existing Author:



            input CreatePostInput {
            title: String
            authorId: ID!
            votes: Int
            }


            of course this is only the interface definition. We need to actually create the Post and link it to the Author inside the resolver.



            The mutation query will look like:



            mutation createPost {
            createPost(input: {
            title: "Title"
            votes: 345
            authorId: "authorId1"
            }) {
            id
            title
            votes
            author {
            id
            firstName
            lastName
            }
            }
            }


            Hope that helps!






            share|improve this answer




























              1














              There are 2 questions in one, so I'll answer in the order there were asked.



              1. return all of the posts associated with an author



              Your schema looks correct. The query would look like:



              query {
              author(id: 1) {
              id
              posts {
              id
              title
              }
              }
              }


              2. create Post and attach to an author



              In your example if you want to expose an interface to create a Post, then you would have to expose a mutation field in your schema



              e.g:



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


              if you want at the same time of creating a post, to also attach it to an author then you could add the authorId as part of the input, here we only want to attach the Post to an existing Author:



              input CreatePostInput {
              title: String
              authorId: ID!
              votes: Int
              }


              of course this is only the interface definition. We need to actually create the Post and link it to the Author inside the resolver.



              The mutation query will look like:



              mutation createPost {
              createPost(input: {
              title: "Title"
              votes: 345
              authorId: "authorId1"
              }) {
              id
              title
              votes
              author {
              id
              firstName
              lastName
              }
              }
              }


              Hope that helps!






              share|improve this answer


























                1












                1








                1







                There are 2 questions in one, so I'll answer in the order there were asked.



                1. return all of the posts associated with an author



                Your schema looks correct. The query would look like:



                query {
                author(id: 1) {
                id
                posts {
                id
                title
                }
                }
                }


                2. create Post and attach to an author



                In your example if you want to expose an interface to create a Post, then you would have to expose a mutation field in your schema



                e.g:



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


                if you want at the same time of creating a post, to also attach it to an author then you could add the authorId as part of the input, here we only want to attach the Post to an existing Author:



                input CreatePostInput {
                title: String
                authorId: ID!
                votes: Int
                }


                of course this is only the interface definition. We need to actually create the Post and link it to the Author inside the resolver.



                The mutation query will look like:



                mutation createPost {
                createPost(input: {
                title: "Title"
                votes: 345
                authorId: "authorId1"
                }) {
                id
                title
                votes
                author {
                id
                firstName
                lastName
                }
                }
                }


                Hope that helps!






                share|improve this answer













                There are 2 questions in one, so I'll answer in the order there were asked.



                1. return all of the posts associated with an author



                Your schema looks correct. The query would look like:



                query {
                author(id: 1) {
                id
                posts {
                id
                title
                }
                }
                }


                2. create Post and attach to an author



                In your example if you want to expose an interface to create a Post, then you would have to expose a mutation field in your schema



                e.g:



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


                if you want at the same time of creating a post, to also attach it to an author then you could add the authorId as part of the input, here we only want to attach the Post to an existing Author:



                input CreatePostInput {
                title: String
                authorId: ID!
                votes: Int
                }


                of course this is only the interface definition. We need to actually create the Post and link it to the Author inside the resolver.



                The mutation query will look like:



                mutation createPost {
                createPost(input: {
                title: "Title"
                votes: 345
                authorId: "authorId1"
                }) {
                id
                title
                votes
                author {
                id
                firstName
                lastName
                }
                }
                }


                Hope that helps!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 1:55









                TinouTinou

                4,15441319




                4,15441319
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421982%2fentity-association-syntax-issue-with-graphql-queries%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    Npm cannot find a required file even through it is in the searched directory