Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batchUrl property of URL middleware is not passed req #36

Closed
chris-verclytte opened this issue Jan 5, 2017 · 4 comments
Closed

batchUrl property of URL middleware is not passed req #36

chris-verclytte opened this issue Jan 5, 2017 · 4 comments

Comments

@chris-verclytte
Copy link

Hello,

I wonder why url property is wrapped with req (https://github.com/nodkz/react-relay-network-layer/blob/master/src/middleware/url.js#L26) allowing for data being passed by another middleware through req but batchUrl is not.
My use case is that I am dynamically retrieving the api url with a middleware, and I would like both url and batchUrl to use the api url from req.

@nodkz
Copy link
Collaborator

nodkz commented Jan 5, 2017

urlMiddleware determines which type of query single or batched via req.relayReqType === 'batch-query' and passes to next middleware just url for fetching (supposing that all other middlewares do not care about url).

So if you want to create more complex logic, do not use urlMiddleware and write your own inline middleware (find example with next => req => {...} in readme.md).

@chris-verclytte
Copy link
Author

I agree with that but I am not sure we are talkin gabout the same thing.
I see this example in the documentation:

urlMiddleware({
    url: (req) => '/graphql',
  }),

and thus would expect this code to work:

urlMiddleware({
    url: (req) => req.someProperty + '/graphql',
    batchUrl: (req) => req.someProperty + '/graphql/batch',
  }),

but it seems that batchUrl does not accept a function.

@nodkz
Copy link
Collaborator

nodkz commented Jan 5, 2017

export default function urlMiddleware(opts = {}) {
  const urlOrThunk = opts.url || '/graphql';
  const batchUrlOrThunk = opts.batchUrl || '/graphql/batch';
  const fetchOpts = opts.opts;

  return next => req => {
    if (fetchOpts) {
      const { headers, ...otherOpts } = fetchOpts;
      Object.assign(req, otherOpts);
      if (headers) {
        Object.assign(req.headers, headers);
      }
    }

    let url;
    if (req.relayReqType === 'batch-query') {
      url = batchUrlOrThunk;
    } else {
      url = urlOrThunk;
    }

    req.url = isFunction(url) ? url(req) : url;

    return next(req);
  };
}

See carefully that url variable assigned to batchUrlOrThunk (opts.batchUrl) or urlOrThunk (opts.url) according to req.relayReqType value. And after that checking is url function or not.

So it should work as you expected.

@chris-verclytte
Copy link
Author

Ok got it, my error should came from another part then. I am closing this now as the behavior is the one expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants