Graphql Dataloader File Structure and ContextWriting files in Node.jsCheck synchronously if file/directory exists in Node.jsWhat's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?Do all nodes in a relay-compliant GraphQL server need to have an id?Apollo GraphQL server; setting context to handle requests triggered by a fired subscriptionWrong order of GraphQL resolver arguments (root, args, context)GraphQL Dataloader not knowing keys in advanceGraphQL DataLoader - Loader per Service Layer vs Per ResolverHow to use Mongoose with GraphQL and DataLoader?Writing direct queries under graphql layer

How is it believable that Euron could so easily pull off this ambush?

Employee is self-centered and affects the team negatively

Antivirus for Ubuntu 18.04

What's the 2-minute timer on mobile Deutsche Bahn tickets?

Appropriate age to involve kids in life changing decisions

Which "exotic salt" can lower water's freezing point by 70 °C?

An adjective or a noun to describe a very small apartment / house etc

My C Drive is full without reason

How do I give a darkroom course without negs from the attendees?

Are modes in jazz primarily a melody thing?

Did Ham the Chimp follow commands, or did he just randomly push levers?

Assuming a normal distribution: what is the sd for a given mean?

Splitting polygons and dividing attribute value proportionally using ArcGIS Pro?

How does jetBlue determine its boarding order?

Explaining intravenous drug abuse to a small child

Why did Dr. Strange keep looking into the future after the snap?

Is it safe to keep the GPU on 100% utilization for a very long time?

Is throwing dice a stochastic or a deterministic process?

What’s the interaction between darkvision and the Eagle Aspect of the beast, if you have Darkvision past 100 feet?

Can a player choose to add detail and flavor to their character's spells and abilities?

The unknown and unexplained in science fiction

Why were the rules for Proliferate changed?

What's weird about Proto-Indo-European Stops?

Why can’t you see at the start of the Big Bang?



Graphql Dataloader File Structure and Context


Writing files in Node.jsCheck synchronously if file/directory exists in Node.jsWhat's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?Do all nodes in a relay-compliant GraphQL server need to have an id?Apollo GraphQL server; setting context to handle requests triggered by a fired subscriptionWrong order of GraphQL resolver arguments (root, args, context)GraphQL Dataloader not knowing keys in advanceGraphQL DataLoader - Loader per Service Layer vs Per ResolverHow to use Mongoose with GraphQL and DataLoader?Writing direct queries under graphql layer






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















Let me preface this by saying I am not a javascript developer, so I'm probably missing something very obvious. I'm a data warehouse developer and creating a graphql server that can communicate with our DW got dropped in my lap.



I've been trying to get dataloaders to work on my graphql server by using a single object in the context, containing multiple dataloaders. I'm then trying to call the appropriate dataloader in the resolver. However, I've been unable to get this to work correctly. The consolidated dataloader object only works if I individually reference the dataloaders in the server context.



I'm trying to follow a similar pattern with the loaders as I have with my models, which is each broken out into a separate file, then consolidated for use as a single object via recursion through the file structure.



Example is I have an object called loaders which contains two loaders: countryLoader and marketsectorLoader, each of which is defined in a separate file under the "loaders" directory. In my server context, the following works



import * as loaders from "./loaders" 
graphQLServer.use('/graphql', bodyParser.json(),
graphqlExpress({
schema,
context: {
countryLoader: loaders.countryLoader()


I can then call this in my resolver:



 StateProvince: 
Country: (parent, args, countryLoader) =>
countryLoader.load(parent.Country_fkey) ,


This functions correctly, batching and returning the correct query result, but I'd prefer not to have to declare each specific dataloader from the loaders object as part of the context. However, I've been unable to figure out the syntax to use the loaders object in the context and call the appropriate
individual dataloader in the appropriate resolver.



I've tried several variants of the following example:
https://github.com/relay-tools/react-relay-network-layer/blob/master/examples/dataLoaderPerBatchRequest.js



which seems to be using the type of technique I'm trying to leverage:



//context snippet:
context:
request: req, // just for example, pass request to context
dataLoaders: initDataLoaders(),
,


However, no luck. I suspect the issue is with my resolver syntax, but I'm not sure, and I haven't been able to find working examples with multiple dataloaders.










share|improve this question




























    1















    Let me preface this by saying I am not a javascript developer, so I'm probably missing something very obvious. I'm a data warehouse developer and creating a graphql server that can communicate with our DW got dropped in my lap.



    I've been trying to get dataloaders to work on my graphql server by using a single object in the context, containing multiple dataloaders. I'm then trying to call the appropriate dataloader in the resolver. However, I've been unable to get this to work correctly. The consolidated dataloader object only works if I individually reference the dataloaders in the server context.



    I'm trying to follow a similar pattern with the loaders as I have with my models, which is each broken out into a separate file, then consolidated for use as a single object via recursion through the file structure.



    Example is I have an object called loaders which contains two loaders: countryLoader and marketsectorLoader, each of which is defined in a separate file under the "loaders" directory. In my server context, the following works



    import * as loaders from "./loaders" 
    graphQLServer.use('/graphql', bodyParser.json(),
    graphqlExpress({
    schema,
    context: {
    countryLoader: loaders.countryLoader()


    I can then call this in my resolver:



     StateProvince: 
    Country: (parent, args, countryLoader) =>
    countryLoader.load(parent.Country_fkey) ,


    This functions correctly, batching and returning the correct query result, but I'd prefer not to have to declare each specific dataloader from the loaders object as part of the context. However, I've been unable to figure out the syntax to use the loaders object in the context and call the appropriate
    individual dataloader in the appropriate resolver.



    I've tried several variants of the following example:
    https://github.com/relay-tools/react-relay-network-layer/blob/master/examples/dataLoaderPerBatchRequest.js



    which seems to be using the type of technique I'm trying to leverage:



    //context snippet:
    context:
    request: req, // just for example, pass request to context
    dataLoaders: initDataLoaders(),
    ,


    However, no luck. I suspect the issue is with my resolver syntax, but I'm not sure, and I haven't been able to find working examples with multiple dataloaders.










    share|improve this question
























      1












      1








      1








      Let me preface this by saying I am not a javascript developer, so I'm probably missing something very obvious. I'm a data warehouse developer and creating a graphql server that can communicate with our DW got dropped in my lap.



      I've been trying to get dataloaders to work on my graphql server by using a single object in the context, containing multiple dataloaders. I'm then trying to call the appropriate dataloader in the resolver. However, I've been unable to get this to work correctly. The consolidated dataloader object only works if I individually reference the dataloaders in the server context.



      I'm trying to follow a similar pattern with the loaders as I have with my models, which is each broken out into a separate file, then consolidated for use as a single object via recursion through the file structure.



      Example is I have an object called loaders which contains two loaders: countryLoader and marketsectorLoader, each of which is defined in a separate file under the "loaders" directory. In my server context, the following works



      import * as loaders from "./loaders" 
      graphQLServer.use('/graphql', bodyParser.json(),
      graphqlExpress({
      schema,
      context: {
      countryLoader: loaders.countryLoader()


      I can then call this in my resolver:



       StateProvince: 
      Country: (parent, args, countryLoader) =>
      countryLoader.load(parent.Country_fkey) ,


      This functions correctly, batching and returning the correct query result, but I'd prefer not to have to declare each specific dataloader from the loaders object as part of the context. However, I've been unable to figure out the syntax to use the loaders object in the context and call the appropriate
      individual dataloader in the appropriate resolver.



      I've tried several variants of the following example:
      https://github.com/relay-tools/react-relay-network-layer/blob/master/examples/dataLoaderPerBatchRequest.js



      which seems to be using the type of technique I'm trying to leverage:



      //context snippet:
      context:
      request: req, // just for example, pass request to context
      dataLoaders: initDataLoaders(),
      ,


      However, no luck. I suspect the issue is with my resolver syntax, but I'm not sure, and I haven't been able to find working examples with multiple dataloaders.










      share|improve this question














      Let me preface this by saying I am not a javascript developer, so I'm probably missing something very obvious. I'm a data warehouse developer and creating a graphql server that can communicate with our DW got dropped in my lap.



      I've been trying to get dataloaders to work on my graphql server by using a single object in the context, containing multiple dataloaders. I'm then trying to call the appropriate dataloader in the resolver. However, I've been unable to get this to work correctly. The consolidated dataloader object only works if I individually reference the dataloaders in the server context.



      I'm trying to follow a similar pattern with the loaders as I have with my models, which is each broken out into a separate file, then consolidated for use as a single object via recursion through the file structure.



      Example is I have an object called loaders which contains two loaders: countryLoader and marketsectorLoader, each of which is defined in a separate file under the "loaders" directory. In my server context, the following works



      import * as loaders from "./loaders" 
      graphQLServer.use('/graphql', bodyParser.json(),
      graphqlExpress({
      schema,
      context: {
      countryLoader: loaders.countryLoader()


      I can then call this in my resolver:



       StateProvince: 
      Country: (parent, args, countryLoader) =>
      countryLoader.load(parent.Country_fkey) ,


      This functions correctly, batching and returning the correct query result, but I'd prefer not to have to declare each specific dataloader from the loaders object as part of the context. However, I've been unable to figure out the syntax to use the loaders object in the context and call the appropriate
      individual dataloader in the appropriate resolver.



      I've tried several variants of the following example:
      https://github.com/relay-tools/react-relay-network-layer/blob/master/examples/dataLoaderPerBatchRequest.js



      which seems to be using the type of technique I'm trying to leverage:



      //context snippet:
      context:
      request: req, // just for example, pass request to context
      dataLoaders: initDataLoaders(),
      ,


      However, no luck. I suspect the issue is with my resolver syntax, but I'm not sure, and I haven't been able to find working examples with multiple dataloaders.







      node.js graphql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 20:09









      ListerDListerD

      215




      215






















          1 Answer
          1






          active

          oldest

          votes


















          0














          If I'm reading your code correctly, importing your loaders using a wildcard import like this:



          import * as loaders from './loaders'


          results in an object wherein each property is a function that creates an instance of a particular DataLoader. So we just need to iterate through each property. For example:



          // Using forEach
          const dataLoaders =
          Object.keys(loaders).forEach(loaderName =>
          dataLoaders[loaderName] = loaders[loaderName]()
          )

          // Or using reduce
          const dataLoaders = Object.keys(loaders).reduce((result, loaderName) =>
          result[loaderName] = loaders[loaderName]()
          return result
          , )


          Using lodash, you can also just do something like:



          const dataLoaders = _.mapValues(loaders, loader => loader())





          share|improve this answer























          • Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

            – ListerD
            Mar 25 at 12:58











          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%2f55307130%2fgraphql-dataloader-file-structure-and-context%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









          0














          If I'm reading your code correctly, importing your loaders using a wildcard import like this:



          import * as loaders from './loaders'


          results in an object wherein each property is a function that creates an instance of a particular DataLoader. So we just need to iterate through each property. For example:



          // Using forEach
          const dataLoaders =
          Object.keys(loaders).forEach(loaderName =>
          dataLoaders[loaderName] = loaders[loaderName]()
          )

          // Or using reduce
          const dataLoaders = Object.keys(loaders).reduce((result, loaderName) =>
          result[loaderName] = loaders[loaderName]()
          return result
          , )


          Using lodash, you can also just do something like:



          const dataLoaders = _.mapValues(loaders, loader => loader())





          share|improve this answer























          • Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

            – ListerD
            Mar 25 at 12:58















          0














          If I'm reading your code correctly, importing your loaders using a wildcard import like this:



          import * as loaders from './loaders'


          results in an object wherein each property is a function that creates an instance of a particular DataLoader. So we just need to iterate through each property. For example:



          // Using forEach
          const dataLoaders =
          Object.keys(loaders).forEach(loaderName =>
          dataLoaders[loaderName] = loaders[loaderName]()
          )

          // Or using reduce
          const dataLoaders = Object.keys(loaders).reduce((result, loaderName) =>
          result[loaderName] = loaders[loaderName]()
          return result
          , )


          Using lodash, you can also just do something like:



          const dataLoaders = _.mapValues(loaders, loader => loader())





          share|improve this answer























          • Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

            – ListerD
            Mar 25 at 12:58













          0












          0








          0







          If I'm reading your code correctly, importing your loaders using a wildcard import like this:



          import * as loaders from './loaders'


          results in an object wherein each property is a function that creates an instance of a particular DataLoader. So we just need to iterate through each property. For example:



          // Using forEach
          const dataLoaders =
          Object.keys(loaders).forEach(loaderName =>
          dataLoaders[loaderName] = loaders[loaderName]()
          )

          // Or using reduce
          const dataLoaders = Object.keys(loaders).reduce((result, loaderName) =>
          result[loaderName] = loaders[loaderName]()
          return result
          , )


          Using lodash, you can also just do something like:



          const dataLoaders = _.mapValues(loaders, loader => loader())





          share|improve this answer













          If I'm reading your code correctly, importing your loaders using a wildcard import like this:



          import * as loaders from './loaders'


          results in an object wherein each property is a function that creates an instance of a particular DataLoader. So we just need to iterate through each property. For example:



          // Using forEach
          const dataLoaders =
          Object.keys(loaders).forEach(loaderName =>
          dataLoaders[loaderName] = loaders[loaderName]()
          )

          // Or using reduce
          const dataLoaders = Object.keys(loaders).reduce((result, loaderName) =>
          result[loaderName] = loaders[loaderName]()
          return result
          , )


          Using lodash, you can also just do something like:



          const dataLoaders = _.mapValues(loaders, loader => loader())






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 23 at 6:01









          Daniel ReardenDaniel Rearden

          18.7k12247




          18.7k12247












          • Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

            – ListerD
            Mar 25 at 12:58

















          • Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

            – ListerD
            Mar 25 at 12:58
















          Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

          – ListerD
          Mar 25 at 12:58





          Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

          – ListerD
          Mar 25 at 12:58



















          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%2f55307130%2fgraphql-dataloader-file-structure-and-context%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript