From 3dc9719268fa75a88cdec704a2d45a247df2b3f6 Mon Sep 17 00:00:00 2001 From: Michael Golfi Date: Tue, 17 Apr 2018 11:39:06 -0700 Subject: [PATCH] Azure Functions now returns correct response body (#753) * Added fixes to apolloAzureFunctions.js and sample functions for the GraphQL API and GraphiQL * Added issue and pr details to changelog * Maintain use of `context.done()`, but use `isRaw` instead. Per the Azure documentation regarding the response object available at: https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#response-object * Switch Azure example to use CommonJS rather than native ESM exports. --- CHANGELOG.md | 1 + .../apollo-server-azure-functions/README.md | 48 ++++++++++++------- .../src/azureFunctionsApollo.ts | 32 +++++++------ 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fde7a3c4ce0..edee246fb83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All of the packages in the `apollo-server` repo are released with the same versi ### vNEXT +* `apollo-server-azure-functions`: Fix non-functional Azure Functions implementation and update examples in Azure Functions' `README.md`. [PR #753](https://github.com/apollographql/apollo-server/pull/753) [Issue #684](https://github.com/apollographql/apollo-server/issues/684) * `apollo-server-adonis`: The `Content-type` of an operation response will now be correctly set to `application/json`. ### v1.3.4 diff --git a/packages/apollo-server-azure-functions/README.md b/packages/apollo-server-azure-functions/README.md index fedbc28c891..e880c91b00a 100755 --- a/packages/apollo-server-azure-functions/README.md +++ b/packages/apollo-server-azure-functions/README.md @@ -3,15 +3,17 @@ title: Azure Functions description: Setting up Apollo Server with Azure Functions --- -[![npm version](https://badge.fury.io/js/apollo-server-core.svg)](https://badge.fury.io/js/apollo-server-core) [![Build Status](https://circleci.com/gh/apollographql/apollo-cache-control-js.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-cache-control-js) [![Coverage Status](https://coveralls.io/repos/github/apollographql/apollo-server/badge.svg?branch=master)](https://coveralls.io/github/apollographql/apollo-server?branch=master) [![Get on Slack](https://img.shields.io/badge/slack-join-orange.svg)](https://www.apollographql.com/#slack) +[![npm version](https://badge.fury.io/js/apollo-server-core.svg)](https://badge.fury.io/js/apollo-server-core) [![Build Status](https://travis-ci.org/apollographql/apollo-server.svg?branch=master)](https://travis-ci.org/apollographql/apollo-server) [![Coverage Status](https://coveralls.io/repos/github/apollographql/apollo-server/badge.svg?branch=master)](https://coveralls.io/github/apollographql/apollo-server?branch=master) [![Get on Slack](https://img.shields.io/badge/slack-join-orange.svg)](https://www.apollographql.com/#slack) This is the Azure Functions integration for the Apollo community GraphQL Server. [Read the docs.](https://www.apollographql.com/docs/apollo-server/) -## Example: +## Sample Code -```js -const server = require('apollo-server-azure-functions'); -const graphqlTools = require('graphql-tools'); +### GraphQL: + +```javascript +const { graphqlAzureFunctions } = require('apollo-server-azure-functions'); +const { makeExecutableSchema } = require('graphql-tools'); const typeDefs = ` type Random { @@ -34,23 +36,35 @@ const resolvers = { }, }; -const schema = graphqlTools.makeExecutableSchema({ +const schema = makeExecutableSchema({ typeDefs, resolvers, }); module.exports = function run(context, request) { - if (request.method === 'POST') { - server.graphqlAzureFunctions({ - endpointURL: '/api/graphql', - schema: schema, - })(context, request); - } else if (request.method === 'GET') { - return server.graphiqlAzureFunctions({ - endpointURL: '/api/graphql', - })(context, request); - } + graphqlAzureFunctions({ schema })(context, request); }; ``` -[Read the CHANGELOG.](https://github.com/apollographql/apollo-server/blob/master/CHANGELOG.md) +### GraphiQL + +```javascript +const { graphiqlAzureFunctions } = require('apollo-server-azure-functions'); + +export function run(context, request) { + let query = ` + { + rands { + id + rand + } + } + `; + + // End point points to the path to the GraphQL API function + graphiqlAzureFunctions({ endpointURL: '/api/graphql', query })( + context, + request, + ); +} +``` diff --git a/packages/apollo-server-azure-functions/src/azureFunctionsApollo.ts b/packages/apollo-server-azure-functions/src/azureFunctionsApollo.ts index 65c1496b4fb..aa20e7c8609 100755 --- a/packages/apollo-server-azure-functions/src/azureFunctionsApollo.ts +++ b/packages/apollo-server-azure-functions/src/azureFunctionsApollo.ts @@ -4,7 +4,10 @@ import { HttpStatusCodes, } from 'azure-functions-typescript'; import { GraphQLOptions, runHttpQuery } from 'apollo-server-core'; -import * as GraphiQL from 'apollo-server-module-graphiql'; +import { + GraphiQLData, + resolveGraphiQLString, +} from 'apollo-server-module-graphiql'; export interface AzureFunctionsGraphQLOptionsFunction { (context: IHttpContext): GraphQLOptions | Promise; @@ -30,8 +33,8 @@ export interface IHeaders { export interface AzureFunctionsGraphiQLOptionsFunction { (context: IHttpContext, request: IFunctionRequest): - | GraphiQL.GraphiQLData - | Promise; + | GraphiQLData + | Promise; } export function graphqlAzureFunctions( @@ -61,13 +64,14 @@ export function graphqlAzureFunctions( return runHttpQuery([httpContext, request], queryRequest) .then(gqlResponse => { const result = { - status: 200, - headers: { 'Content-Type': 'application/json' }, + status: HttpStatusCodes.OK, + headers: { + 'Content-Type': 'application/json', + }, body: gqlResponse, + isRaw: true, }; - httpContext.res = result; - httpContext.done(null, result); }) .catch(error => { @@ -78,7 +82,6 @@ export function graphqlAzureFunctions( }; httpContext.res = result; - httpContext.done(null, result); }); }; @@ -96,22 +99,23 @@ export function graphqlAzureFunctions( */ export function graphiqlAzureFunctions( - options: GraphiQL.GraphiQLData | AzureFunctionsGraphiQLOptionsFunction, + options: GraphiQLData | AzureFunctionsGraphiQLOptionsFunction, ) { return (httpContext: IHttpContext, request: IFunctionRequest) => { const query = request.query; - GraphiQL.resolveGraphiQLString(query, options, httpContext, request).then( + resolveGraphiQLString(query, options, httpContext, request).then( graphiqlString => { - httpContext.res = { - status: 200, + const result = { + status: HttpStatusCodes.OK, headers: { 'Content-Type': 'text/html', }, body: graphiqlString, + isRaw: true, }; - - httpContext.done(null, httpContext.res); + httpContext.res = result; + httpContext.done(null, result); }, error => { httpContext.res = {