GithubHelp home page GithubHelp logo

Comments (12)

kratam avatar kratam commented on April 27, 2024 9

Yes, thank you! For anyone else having the same problem, it should look like this:

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

// create transforms array for transformSchema
const transforms = [new ReplaceFieldWithFragment(schema, fragmentReplacements)]
// apply transformations
const finalSchema = transformSchema(schema, transforms)

from graphql-shield.

maticzav avatar maticzav commented on April 27, 2024 5

Hey @nemcek πŸ‘‹

That's an interesting question, and indeed you are right! The current version doesn't support fragments. Fragments are a way of telling graphql resolver which information your resolver requires to work correctly.

The next version of shield should introduce this with the following syntax. Right now, we fully depend on graphql-tools PR and grpahql-middleware fragments support which should land in no time.

const nestedRule = rule({
  cache: 'strict',
  fragment: 'fragment UserID on User { id }',
})(async ({ id }, args, ctx, info) => {
  return somethingWithID(id)
})

I hope this covers your idea. Tell me if you have any thoughts on this as well! πŸ™‚

from graphql-shield.

maticzav avatar maticzav commented on April 27, 2024 1

Hey @kratam πŸ‘‹

Could you also provide schema and rules? Also, you should note thay you have to use fragmentReplacements returned by middleware; maybe we should indicate that in the docs.

from graphql-shield.

lastmjs avatar lastmjs commented on April 27, 2024 1

What libraries are transformSchema and ReplaceFieldWithFragment from?

from graphql-shield.

lastmjs avatar lastmjs commented on April 27, 2024 1

I believe transformSchema from graphql-tools is now called wrapSchema

from graphql-shield.

nemcek avatar nemcek commented on April 27, 2024

Oh, sweet!

Just to be sure I understood it correctly. In the next version if I use rule with a fragment, for example as:

const isOwner = rule({
    fragment: 'fragment UserID on User { id }',
})(async (parent, args, ctx, info) => {
  return ctx.user.items.some(id => id === parent.id)
})

then I can be sure that parent.id will be populated, right?
If that's the case then shield solves all my use-cases. Love it!

from graphql-shield.

maticzav avatar maticzav commented on April 27, 2024

Yeah, that’s exactly that! Perfect πŸš€πŸŽ‰

from graphql-shield.

kratam avatar kratam commented on April 27, 2024

Is it live? parent is always undefined even when I set cache: 'strict' (the fragment part doesn't even matter):

const isOwner = rule({
  cache: 'strict',
  fragment: 'fragment Id on Item { id }',
})(async (parent, args, ctx: Context, info) => {
  console.log(parent) // undefined
  return true
})

Note: I'm using:

"apollo-server-express": "^2.0.5",
"graphql-middleware": "^1.7.1",
"graphql-shield": "^3.2.0",

from graphql-shield.

kratam avatar kratam commented on April 27, 2024

Wow, super fast response! :) How can I pass fragmentReplacements to ApolloServer?

from graphql-shield.

maticzav avatar maticzav commented on April 27, 2024

Hey πŸ‘‹

Sorry for the delayed response. fragmentReplacements are part of schema forwarding; graphql-binding supports this using fragmentReplacements parameter.

Besides that, you could also use standalone transforms which are supported by ApolloServer, especially ReplaceFieldWithFragment

https://www.apollographql.com/docs/graphql-tools/schema-transforms.html#Other

I hope this helps you with your problem πŸ™‚

from graphql-shield.

forrestwilkins avatar forrestwilkins commented on April 27, 2024

It looks like ReplaceFieldWithFragment should be accessible from @graphql-tools/delegate according to this thread, but I'm getting the following error:
Module '"@graphql-tools/delegate"' has no exported member 'ReplaceFieldWithFragment'

from graphql-shield.

forrestwilkins avatar forrestwilkins commented on April 27, 2024

Has anyone been able to configure fragmentReplacements for NestJS and Apollo Server? It looks like ReplaceFieldWithFragment is deprecated now.

from graphql-shield.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.