Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'graphql-playground-middleware-lambda' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
import playground from 'graphql-playground-middleware-lambda'
import { GITHUB_OAUTH_TOKEN, REACT_APP_GRAPHQL_ENDPOINT } from '../config/env'
const headers: { [key: string]: string } = {}
if (!REACT_APP_GRAPHQL_ENDPOINT) {
throw new Error(`You must define REACT_APP_GRAPHQL_ENDPOINT env in order to GraphQL Playground`)
}
// fulfil headers for Playground requests. Not strictly required, can be
// altered through the Playground Settings UI.
if (GITHUB_OAUTH_TOKEN) {
headers.authorization = `bearer ${GITHUB_OAUTH_TOKEN}`
}
const handle = playground({ endpoint: REACT_APP_GRAPHQL_ENDPOINT, headers })
/**
* Simple wrapper to avoid duplicated response (callback + return).
*/
const handler: Handler = (lambdaEvent, context, callback) => {
handle(lambdaEvent, context, callback)
}
export { handler }
if (output) {
output.headers['Access-Control-Allow-Origin'] = '*';
log.info(output.body);
} else {
log.error(error);
}
callback(error, output);
}
const handler = graphqlLambda({ schema: myGraphQLSchema, tracing: true });
return handler(event, context, callbackFilter);
};
// for local endpointURL is /graphql and for prod it is /stage/graphql
exports.playgroundHandler = lambdaPlayground({
endpoint: process.env.REACT_APP_GRAPHQL_ENDPOINT ? process.env.REACT_APP_GRAPHQL_ENDPOINT : '/production/graphql'
});
}
exports.graphqlHandler = function graphqlHandler(event, context, callback) {
function callbackFilter(error, output) {
// eslint-disable-next-line no-param-reassign
output.headers['Access-Control-Allow-Origin'] = '*'
callback(error, output)
}
const myGraphQLSchema = makeExecutableSchema({ typeDefs, resolvers })
const handler = graphqlLambda({ schema: myGraphQLSchema })
return handler(event, context, callbackFilter)
}
exports.playgroundHandler = lambdaPlayground({
endpoint: '/dev',
})
try {
JSON.parse(event.body);
} catch (err) {
const msg = 'Invalid JSON';
log(msg, err);
return callback(null, {
body: msg,
statusCode: 422
});
}
return handler(event, context, callbackFilter);
};
exports.apiHandler = lambdaPlayground({
endpoint: process.env.GRAPHQL_ENDPOINT_URL
? process.env.GRAPHQL_ENDPOINT_URL
: '/production/graphql'
});
}
const { statusCode, headers, result: body } = await server.inject({
method,
url,
payload,
headers: reqHeaders,
validate: false
})
delete headers[`content-encoding`]
delete headers[`transfer-encoding`]
response(null, { statusCode, headers, body })
}
exports.playground = lambdaPlayground({
endpoint: `/graphql`
})
import GraphQLPlayground from 'graphql-playground-middleware-lambda'
export const handler = GraphQLPlayground({ endpoint: '/' })