Graphql-shield: Improve docs on Fragments, how exactly is it working

Created on 18 Dec 2019  路  5Comments  路  Source: maticzav/graphql-shield

Hey, so I am relying on data from the database to verify certain fields, so it seems using fragments would be beneficial for my use case. However I am not really sure how they work or what they do.

So there is this in the docs:

const isItemOwner = rule({
  cache: 'strict',
  fragment: 'fragment ItemID on Item { id }',
})(async ({ id }, args, ctx, info) => {
  return ctx.db.exists.Item({
    id,
    owner: { id: ctx.user.id },
  })
})

Does it provide a way to cache the data you get from the Item query or what is it doing here?

Also why are you getting id from the parent argument and not from args ? I would be happy if I could get some explanation of how exacyly fragments work together with graphql-shield.

(I know what fragments in general are, (I think))

Most helpful comment

All 5 comments

Hey @BjoernRave :wave:,

Thank you for opening an issue. We will get back to you as soon as we can. Also, check out our Open Collective and consider contributing financially.

https://opencollective.com/graphql-shield

PS.: We offer priority support for all financial contributors. Don't forget to add priority label once you start contributing :smile:

ItHi @BjoernRave 馃憢 ,

The idea behind fragments is that you can supply additional information to graphql-binding so that you could get, say id, even when frontend didn't request it.

I am not sure whether there's any benefit from using it; it serves the purpose I mentioned, but I have no idea how to use it in other contexts. All in all, it dumps results in { fragmentReplacements, schema } when you use applyMiddleware like that:

const { schema, fragmentReplacements } = applyMiddleware(schema, permissions)

It makes sure that based on where you used a particular rule with the fragment, it generated a correlating instance of a FragmentReplacement.

I hope that gives you a bit of context. 馃檪

@maticzav Do you have an example repo handy/available to share? I find the https://github.com/maticzav/graphql-shield#fragments section a bit confusing and think it'd be helpful to see the other bits referenced in the code snippet from that section.

Thanks!

@maticzav That does, thank you. I've been using prisma-client (which doesnt support the fragment option) and need to investigate switching over to the prisma-binding (which does)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tnolan8 picture tnolan8  路  5Comments

mhaagens picture mhaagens  路  7Comments

devautor picture devautor  路  7Comments

OGNeutron picture OGNeutron  路  4Comments

nolandg picture nolandg  路  5Comments