how to return customize data in prisma subscription











up vote
1
down vote

favorite












I am learning graphql & prisma and I came across a question on prisma subscription.



I want to return an item list whenever there is a creation or update on Item. So this is my code which not works.



scheme.graphql



# import Item from "./generated/prisma.graphql"

type Subscription {
todoItems: TodoItems
}

type TodoItems {
items: [Item!]!
}


resolver



const Subscription = {
todoItems: {
subscribe: async (parent, args, context, info) => {
const itemSubscription = await context.db.subscription.item({
where: { mutation_in: ['CREATED', 'UPDATED'] },
}, info);

return itemSubscription;
},
resolve: async (payload, args, context, info) => {
const items = await context.db.query.items({ type: 0, orderBy: 'updatedAt_DESC' }, info);
return { items };
},
},
}

module.exports = {
Subscription,
}


and in graphql playground,



subscription{
todoItems{
items{
title
}
}
}


it gives the error:



{
"errors": [
{
"message": "Anonymous Subscription must select only one top level field.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"todoItems"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"Error: Anonymous Subscription must select only one top level field.",
" at asErrorInstance (d:\git\inote\node_modules\graphql\execution\execute.js:489:43)",
" at <anonymous>",
" at process._tickCallback (internal/process/next_tick.js:118:7)"
]
}
}
}
]
}


Any idea?










share|improve this question


























    up vote
    1
    down vote

    favorite












    I am learning graphql & prisma and I came across a question on prisma subscription.



    I want to return an item list whenever there is a creation or update on Item. So this is my code which not works.



    scheme.graphql



    # import Item from "./generated/prisma.graphql"

    type Subscription {
    todoItems: TodoItems
    }

    type TodoItems {
    items: [Item!]!
    }


    resolver



    const Subscription = {
    todoItems: {
    subscribe: async (parent, args, context, info) => {
    const itemSubscription = await context.db.subscription.item({
    where: { mutation_in: ['CREATED', 'UPDATED'] },
    }, info);

    return itemSubscription;
    },
    resolve: async (payload, args, context, info) => {
    const items = await context.db.query.items({ type: 0, orderBy: 'updatedAt_DESC' }, info);
    return { items };
    },
    },
    }

    module.exports = {
    Subscription,
    }


    and in graphql playground,



    subscription{
    todoItems{
    items{
    title
    }
    }
    }


    it gives the error:



    {
    "errors": [
    {
    "message": "Anonymous Subscription must select only one top level field.",
    "locations": [
    {
    "line": 2,
    "column": 3
    }
    ],
    "path": [
    "todoItems"
    ],
    "extensions": {
    "code": "INTERNAL_SERVER_ERROR",
    "exception": {
    "stacktrace": [
    "Error: Anonymous Subscription must select only one top level field.",
    " at asErrorInstance (d:\git\inote\node_modules\graphql\execution\execute.js:489:43)",
    " at <anonymous>",
    " at process._tickCallback (internal/process/next_tick.js:118:7)"
    ]
    }
    }
    }
    ]
    }


    Any idea?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am learning graphql & prisma and I came across a question on prisma subscription.



      I want to return an item list whenever there is a creation or update on Item. So this is my code which not works.



      scheme.graphql



      # import Item from "./generated/prisma.graphql"

      type Subscription {
      todoItems: TodoItems
      }

      type TodoItems {
      items: [Item!]!
      }


      resolver



      const Subscription = {
      todoItems: {
      subscribe: async (parent, args, context, info) => {
      const itemSubscription = await context.db.subscription.item({
      where: { mutation_in: ['CREATED', 'UPDATED'] },
      }, info);

      return itemSubscription;
      },
      resolve: async (payload, args, context, info) => {
      const items = await context.db.query.items({ type: 0, orderBy: 'updatedAt_DESC' }, info);
      return { items };
      },
      },
      }

      module.exports = {
      Subscription,
      }


      and in graphql playground,



      subscription{
      todoItems{
      items{
      title
      }
      }
      }


      it gives the error:



      {
      "errors": [
      {
      "message": "Anonymous Subscription must select only one top level field.",
      "locations": [
      {
      "line": 2,
      "column": 3
      }
      ],
      "path": [
      "todoItems"
      ],
      "extensions": {
      "code": "INTERNAL_SERVER_ERROR",
      "exception": {
      "stacktrace": [
      "Error: Anonymous Subscription must select only one top level field.",
      " at asErrorInstance (d:\git\inote\node_modules\graphql\execution\execute.js:489:43)",
      " at <anonymous>",
      " at process._tickCallback (internal/process/next_tick.js:118:7)"
      ]
      }
      }
      }
      ]
      }


      Any idea?










      share|improve this question













      I am learning graphql & prisma and I came across a question on prisma subscription.



      I want to return an item list whenever there is a creation or update on Item. So this is my code which not works.



      scheme.graphql



      # import Item from "./generated/prisma.graphql"

      type Subscription {
      todoItems: TodoItems
      }

      type TodoItems {
      items: [Item!]!
      }


      resolver



      const Subscription = {
      todoItems: {
      subscribe: async (parent, args, context, info) => {
      const itemSubscription = await context.db.subscription.item({
      where: { mutation_in: ['CREATED', 'UPDATED'] },
      }, info);

      return itemSubscription;
      },
      resolve: async (payload, args, context, info) => {
      const items = await context.db.query.items({ type: 0, orderBy: 'updatedAt_DESC' }, info);
      return { items };
      },
      },
      }

      module.exports = {
      Subscription,
      }


      and in graphql playground,



      subscription{
      todoItems{
      items{
      title
      }
      }
      }


      it gives the error:



      {
      "errors": [
      {
      "message": "Anonymous Subscription must select only one top level field.",
      "locations": [
      {
      "line": 2,
      "column": 3
      }
      ],
      "path": [
      "todoItems"
      ],
      "extensions": {
      "code": "INTERNAL_SERVER_ERROR",
      "exception": {
      "stacktrace": [
      "Error: Anonymous Subscription must select only one top level field.",
      " at asErrorInstance (d:\git\inote\node_modules\graphql\execution\execute.js:489:43)",
      " at <anonymous>",
      " at process._tickCallback (internal/process/next_tick.js:118:7)"
      ]
      }
      }
      }
      ]
      }


      Any idea?







      graphql prisma






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      39ecneret

      1361314




      1361314
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Prisma does not support subscribing item lists. Instead, prisma wants you to subscribe to single item mutations ("created", "updated", "deleted"). As described here.



          E.g.



          subscription newTodos {
          todo(where: {
          mutation_in: [CREATED]
          }) {
          mutation
          node {
          title
          }
          }
          }


          To get "the full list", you have to query on the todos after subscribing to avoid missing events (race condition). As a result you have to manually "sync" the data from the subscription and your query.






          share|improve this answer





















          • Thanks for the answer! I just want to share this tutorial as a thorough intro to implementing subscriptions with Prisma.
            – nburk
            yesterday











          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',
          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%2f53371234%2fhow-to-return-customize-data-in-prisma-subscription%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








          up vote
          2
          down vote



          accepted










          Prisma does not support subscribing item lists. Instead, prisma wants you to subscribe to single item mutations ("created", "updated", "deleted"). As described here.



          E.g.



          subscription newTodos {
          todo(where: {
          mutation_in: [CREATED]
          }) {
          mutation
          node {
          title
          }
          }
          }


          To get "the full list", you have to query on the todos after subscribing to avoid missing events (race condition). As a result you have to manually "sync" the data from the subscription and your query.






          share|improve this answer





















          • Thanks for the answer! I just want to share this tutorial as a thorough intro to implementing subscriptions with Prisma.
            – nburk
            yesterday















          up vote
          2
          down vote



          accepted










          Prisma does not support subscribing item lists. Instead, prisma wants you to subscribe to single item mutations ("created", "updated", "deleted"). As described here.



          E.g.



          subscription newTodos {
          todo(where: {
          mutation_in: [CREATED]
          }) {
          mutation
          node {
          title
          }
          }
          }


          To get "the full list", you have to query on the todos after subscribing to avoid missing events (race condition). As a result you have to manually "sync" the data from the subscription and your query.






          share|improve this answer





















          • Thanks for the answer! I just want to share this tutorial as a thorough intro to implementing subscriptions with Prisma.
            – nburk
            yesterday













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Prisma does not support subscribing item lists. Instead, prisma wants you to subscribe to single item mutations ("created", "updated", "deleted"). As described here.



          E.g.



          subscription newTodos {
          todo(where: {
          mutation_in: [CREATED]
          }) {
          mutation
          node {
          title
          }
          }
          }


          To get "the full list", you have to query on the todos after subscribing to avoid missing events (race condition). As a result you have to manually "sync" the data from the subscription and your query.






          share|improve this answer












          Prisma does not support subscribing item lists. Instead, prisma wants you to subscribe to single item mutations ("created", "updated", "deleted"). As described here.



          E.g.



          subscription newTodos {
          todo(where: {
          mutation_in: [CREATED]
          }) {
          mutation
          node {
          title
          }
          }
          }


          To get "the full list", you have to query on the todos after subscribing to avoid missing events (race condition). As a result you have to manually "sync" the data from the subscription and your query.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          lipp

          2,4981818




          2,4981818












          • Thanks for the answer! I just want to share this tutorial as a thorough intro to implementing subscriptions with Prisma.
            – nburk
            yesterday


















          • Thanks for the answer! I just want to share this tutorial as a thorough intro to implementing subscriptions with Prisma.
            – nburk
            yesterday
















          Thanks for the answer! I just want to share this tutorial as a thorough intro to implementing subscriptions with Prisma.
          – nburk
          yesterday




          Thanks for the answer! I just want to share this tutorial as a thorough intro to implementing subscriptions with Prisma.
          – nburk
          yesterday


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371234%2fhow-to-return-customize-data-in-prisma-subscription%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python