Coder Social home page Coder Social logo

Fragments about graphql-tag HOT 3 CLOSED

apollographql avatar apollographql commented on August 29, 2024 1
Fragments

from graphql-tag.

Comments (3)

stubailo avatar stubailo commented on August 29, 2024 2

Not right now, I had some ideas here: apollographql/apollo-client#994 (comment)

from graphql-tag.

jtmthf avatar jtmthf commented on August 29, 2024

I've been hacking around on this and have a half-baked solution using the loader and @stubailo's idea of using a directive.

What works well is first declaring the directive in your schema. I've been using this declaration so far,

# Import a fragment from an external file
directive @import(
    # Path to import fragment from
    from: String!
  ) on FRAGMENT_SPREAD

As far as I can tell, graphql-js only provides a way to define custom directives, but not their runtime. This is fine though as @import would have no runtime implications. So far testing this on graphiql works great, and I assume it would on any other tool that reads a schema such as apollo-codegen and eslint-plugin-graphql.

Right now I have the loader set to parse the query using gql as it always has, but then visit the AST using something like this,

visit(ast, {
  Directive: {
    enter(node) {
      if (node.name.value === 'import') {
        const fromArg = node.arguments.filter(arg => arg.name.value === 'from')
        addImport(fromArg.value.value)
      }
      return node;
    }
  }
});

One thing to note is that the loader currently doesn't strip @import from the query. It doesn't really hurt or help to do so, although I'd appreciate some feedback on which approach is best.

Lastly, a require() is inserted for each import which brings the final emit to look something like,

`var fragments = ${imports.map(path => `require(${path})`)}
  
var query ${JSON.stringify(gql`${source}`)};

query.documents = query.documents.concat(fragments)

module.exports = query`

Overall not too complicated of a loader. Biggest concern of mine is if custom directives are considered taboo still, or if they are safe to implement.

from graphql-tag.

pasiba avatar pasiba commented on August 29, 2024

Good.

from graphql-tag.

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.