Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function testApp(el, {typeDefs, resolvers}, enhanceApp) {
const port = await getPort();
const endpoint = `http://localhost:${port}/graphql`;
const app = new App(el);
const schema = makeExecutableSchema({typeDefs, resolvers});
const client = new ApolloClient({
cache: new InMemoryCache({
addTypename: false,
}).restore({}),
link: new HttpLink({
endpoint,
fetch: async (url, options) => {
// required since the url here is only the path
const result = await fetch(endpoint, options);
return result;
},
}),
});
app.enhance(RenderToken, ApolloRenderEnhancer);
app.register(GraphQLSchemaToken, schema);
app.register(ApolloClientToken, ApolloClientPlugin);
if (enhanceApp) {
export default async function serverRender(req, res) {
const location = req.url;
const apolloClient = new ApolloClient({
ssrMode: true,
link: new HttpLink({ uri, fetch }),
cache: new InMemoryCache(),
});
const context = {};
let store = configureStore();
const sheet = new ServerStyleSheet();
console.log('serverRendering');
// just for demo, replace with a "usefull" async. action to feed your state
try {
const { info } = await fakeFetch();
const currentTime = format(new Date());
const currentState = store.getState();
applyMiddleware(req, next) {
if (!req.options.headers) {
req.options.headers = {}; // Create the header object if needed.
}
const token = AuthService.getJwtToken();
if (token) {
req.options.headers['authorization'] = AuthService.getJwtToken();
console.log('got token: ', token);
} else {
console.log('did not get token', console.log(localStorage.getItem('id_token')));
}
next();
}
}]);
const client = new ApolloClient({ networkInterface });
export function getClient(): ApolloClient {
return client;
}
// _root: any,
// _args: { [key: string]: any },
// _context: any,
// _info: GraphQLResolveInfo,
// ): any => {
// const selections = computeIncludes(_info.operation.selectionSet.selections[0], 'GraphQLDirective');
// console.info('selections');
// console.info(selections);
// console.info(JSON.stringify(selections));
// console.log(_root);
// console.log(_args);
// console.log(_context);
// console.log(_info);
// });
// addResolvers('GraphQLDirective', resolversMap);
const client = new ApolloClient({
link: new SchemaLink({ schema: schema }),
cache: new InMemoryCache(),
connectToDevTools: true
});
client.initQueryManager();
const createPost = gql`
mutation createPost($title: String!) {
createPost(title: $title) {
id
}
}
`;
const createUser = gql`
mutation createUser($name: String!) {
attempts: {
max: MAX_RETRIES,
retryIf: error => !!error
}
}),
errorLink,
new HttpLink({
uri: API_URL,
credentials: DEBUG ? "include" : "same-origin",
headers: {
"x-csrftoken": Cookies.get("csrftoken")
}
})
]);
const apolloClient = new ApolloClient({
link,
cache: new InMemoryCache(),
connectToDevTools: !DEBUG
});
const apolloProvider = new VueApollo({
defaultClient: apolloClient
});
Vue.use(VueApollo);
/* eslint-disable no-new */
new Vue({
el: "#app",
router,
apolloProvider,
networkInterfaceOptions.headers["content-type"] =
"application/x-www-form-urlencoded";
networkInterfaceOptions.useGETForQueries = true;
}
if (
typeof window !== "undefined" &&
window.nuk &&
!options.skipAuthorization
) {
const acsTnlCookie = window.nuk.getCookieValue("acs_tnl");
const sacsTnlCookie = window.nuk.getCookieValue("sacs_tnl");
networkInterfaceOptions.headers.Authorization = `Cookie acs_tnl=${acsTnlCookie};sacs_tnl=${sacsTnlCookie}`;
}
return new ApolloClient({
cache: new InMemoryCache({ fragmentMatcher }).restore(
options.initialState || {}
),
link: createHttpLink(networkInterfaceOptions)
});
};
constructor(
public pg: db.Knex = db.getConnection(),
schema: GraphQLSchema = executableSchema
) {
this.apolloClient = new ApolloClient({
ssrMode: true,
cache: new InMemoryCache(),
link: new SchemaLink({
schema: schema,
context: this
})
});
}
export function createApolloClient({ schema, context, fragmentTypes }) {
return new ApolloClient({
ssrMode: true,
link: ApolloLink.from([
new ValidateLink({ schema }),
new SchemaLink({ schema, context }),
]),
cache: new InMemoryCache({
fragmentMatcher: new IntrospectionFragmentMatcher({
introspectionQueryResultData: fragmentTypes,
}),
}),
})
}
const httpLink = new HttpLink({
uri: 'http://localhost:4000/'
})
const authLink = setContext(async (_, { headers }) => {
const token = await AsyncStorage.getItem('@App:token')
return {
headers: {
...headers,
authorization: token
}
}
})
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
})
export default client