-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66be1fc
commit 0dacaeb
Showing
5 changed files
with
2,223 additions
and
2,084 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,28 @@ | ||
import { ApolloClient } from 'apollo-client' | ||
import { InMemoryCache, NormalizedCacheObject } from 'apollo-cache-inmemory' | ||
import { HttpLink } from 'apollo-link-http' | ||
import fetch from 'isomorphic-unfetch' | ||
import { ApolloClient } from "apollo-client"; | ||
import { InMemoryCache, NormalizedCacheObject } from "apollo-cache-inmemory"; | ||
import { HttpLink } from "apollo-link-http"; | ||
import { ApolloLink } from "apollo-link"; | ||
import errorLink from "./links/error"; | ||
import fetch from "isomorphic-unfetch"; | ||
|
||
export default function createApolloClient(graphqlUrl: string, initialState: NormalizedCacheObject, ctx: Object) { | ||
export default function createApolloClient( | ||
graphqlUrl: string, | ||
initialState: NormalizedCacheObject, | ||
ctx: Object | ||
) { | ||
// The `ctx` (NextPageContext) will only be present on the server. | ||
// use it to extract auth headers (ctx.req) or similar. | ||
return new ApolloClient({ | ||
name: "default-client", | ||
ssrMode: Boolean(ctx), | ||
link: new HttpLink({ | ||
uri: graphqlUrl, // Server URL (must be absolute) | ||
credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers` | ||
fetch, | ||
}), | ||
link: ApolloLink.from([ | ||
errorLink, | ||
new HttpLink({ | ||
uri: graphqlUrl, // Server URL (must be absolute) | ||
credentials: "same-origin", // Additional fetch() options like `credentials` or `headers` | ||
fetch, | ||
}), | ||
]), | ||
cache: new InMemoryCache().restore(initialState), | ||
}) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { onError } from "apollo-link-error"; | ||
import { GraphQLError } from "graphql"; | ||
|
||
const locationsToStr = (locations: GraphQLError["locations"] = []) => | ||
locations.map(({ column, line }) => `line ${line}, col ${column}`).join(";"); | ||
|
||
const errorLink = onError((error) => { | ||
const { graphQLErrors, networkError } = error; | ||
if (graphQLErrors) | ||
graphQLErrors.forEach(({ message, locations, path }) => { | ||
// eslint-disable-next-line no-console | ||
console.log( | ||
`[GraphQL error]: Message: ${message}, Location: ${locationsToStr( | ||
locations | ||
)}, Path: ${path}` | ||
); | ||
}); | ||
if (networkError) { | ||
// eslint-disable-next-line no-console | ||
console.log(`[Network error]: ${networkError}`); | ||
} | ||
}); | ||
|
||
export default errorLink; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.