Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
task: async (ctx, task) => {
task.title = `Publishing ${getIdFromKey(
ctx.currentSchema.engineKey
)} to Apollo Engine`;
const gitContext = await gitInfo();
const variables = {
schema: (await graphql(ctx.schema, parse(introspectionQuery))).data!
.__schema,
tag,
gitContext,
id: getIdFromKey(ctx.currentSchema.engineKey)
};
ctx.current = await toPromise(
execute(engineLink, {
query: UPLOAD_SCHEMA,
variables,
context: {
headers: { ["x-api-key"]: ctx.currentSchema.engineKey },
...(ctx.config.engineEndpoint && {
uri: ctx.config.engineEndpoint
})
}
})
)
.then(async ({ data, errors }) => {
// XXX better end user error message
if (errors) {
console.log(errors);
throw new Error(
};
if (url.startsWith("https:") && skipSSLValidation) {
options.fetchOptions = {
agent: new HTTPSAgent({ rejectUnauthorized: false })
};
}
const getFederationInfoQuery = `
query getFederationInfo {
_service {
sdl
}
}
`;
const { data, errors } = (await toPromise(
linkExecute(createHttpLink(options), {
query: parse(getFederationInfoQuery),
context: { headers }
})
)) as ExecutionResult<{ _service: FederationInfo }>;
if (errors && errors.length) {
// XXX better error handling of GraphQL errors
throw new Error(errors.map(({ message }: Error) => message).join("\n"));
}
if (!data || !data._service) {
throw new Error(
"No data received from server when querying for _service."
);
}
if (!ctx.manifest.operations || !ctx.manifest.operations.length) {
throw new Error("No operations were found.");
}
const variables = {
clientIdentity: {
name: flags.clientName,
identifier: flags.clientIdentifier || flags.clientName,
version: flags.clientVersion
},
serviceId: getIdFromKey(ctx.currentSchema.engineKey),
operations: ctx.manifest.operations
};
await toPromise(
execute(engineLink, {
query: REGISTER_OPERATIONS,
variables,
context: {
headers: { ["x-api-key"]: ctx.currentSchema.engineKey },
...(ctx.config.engineEndpoint && {
uri: ctx.config.engineEndpoint
})
}
})
)
.then(({ data, errors }) => {
// XXX better end user error message
if (!errors) {
return data!.service.registerOperations;
}
this.error(
"No API key was specified. Set an Apollo Engine API key using the `--key` flag or the `ENGINE_API_KEY` environment variable."
);
}
const gitContext = await gitInfo();
const variables = {
id: getIdFromKey(ctx.documentSets[0].engineKey),
// XXX hardcoded for now
tag: "current",
gitContext,
operations: ctx.operations
};
ctx.changes = await toPromise(
execute(engineLink, {
query: VALIDATE_OPERATIONS,
variables,
context: {
headers: { ["x-api-key"]: ctx.documentSets[0].engineKey },
...(ctx.config.engineEndpoint && {
uri: ctx.config.engineEndpoint
})
}
})
)
.then(({ data, errors }) => {
// XXX better end user error message
if (errors)
throw new Error(
errors.map(({ message }) => message).join("\n")
async resolveSchema() {
if (this.schema) return this.schema;
const { skipSSLValidation, url, headers } = this.config;
const options: HttpLink.Options = {
uri: url,
fetch
};
if (url.startsWith("https:") && skipSSLValidation) {
options.fetchOptions = {
agent: new HTTPSAgent({ rejectUnauthorized: false })
};
}
const { data, errors } = (await toPromise(
linkExecute(createHttpLink(options), {
query: parse(getIntrospectionQuery()),
context: { headers }
})
).catch(e => {
// html response from introspection
if (isString(e.message) && e.message.includes("token <")) {
throw new Error(
"Apollo tried to introspect a running GraphQL service at " +
url +
"\nIt expected a JSON schema introspection result, but got an HTML response instead." +
"\nYou may need to add headers to your request or adjust your endpoint url.\n" +
"-----------------------------\n" +
"For more information, please refer to: https://bit.ly/2ByILPj \n\n" +
"The following error occurred:\n-----------------------------\n" +
e.message
task: async ctx => {
const gitContext = await gitInfo();
const variables = {
id: getIdFromKey(ctx.currentSchema.engineKey),
schema: ctx.schema,
// XXX hardcoded for now
tag: "current",
gitContext
};
ctx.changes = await toPromise(
execute(engineLink, {
query: VALIDATE_SCHEMA,
variables,
context: {
headers: { ["x-api-key"]: ctx.currentSchema.engineKey },
...(ctx.config.engineEndpoint && {
uri: ctx.config.engineEndpoint
})
}
})
)
.then(({ data, errors }) => {
// XXX better end user error message
if (errors)
throw new Error(
errors.map(({ message }) => message).join("\n")
};
if (url.startsWith("https:") && skipSSLValidation) {
options.fetchOptions = {
agent: new HTTPSAgent({ rejectUnauthorized: false })
};
}
const getFederationInfoQuery = `
query getFederationInfo {
_service {
sdl
}
}
`;
const { data, errors } = (await toPromise(
linkExecute(createHttpLink(options), {
query: parse(getFederationInfoQuery),
context: { headers }
})
)) as ExecutionResult<{ _service: { sdl: string } }>;
if (errors && errors.length) {
return Debug.error(
errors.map(({ message }: Error) => message).join("\n")
);
}
if (!data || !data._service) {
return Debug.error(
"No data received from server when querying for _service."
);
export async function fetchSchemaFromEngine({
apiKey,
tag = "current",
customEngine
}: {
apiKey: string;
tag?: string;
customEngine?: string;
}): Promise {
const variables = {
id: getIdFromKey(apiKey as string),
tag
};
const engineSchema = await toPromise(
linkExecute(engineLink, {
query: SCHEMA_QUERY,
variables,
context: {
headers: { ["x-api-key"]: apiKey },
...(customEngine && { uri: customEngine })
}
})
);
if (!engineSchema.data || !engineSchema.data.service.schema) {
throw new Error("Unable to get schema from Apollo Engine");
}
return engineSchema.data.service.schema.__schema;
}
const urlObject = new URL(url);
const host = urlObject.host;
const port = +urlObject.port || 443;
const agentOptions: AgentOptions = {
host: host,
port: port,
rejectUnauthorized: false
};
const agent = new Agent(agentOptions);
options.fetchOptions = { agent: agent };
}
const { data, errors }: any = await toPromise(
linkExecute(createHttpLink(options), {
query: introspection,
context: { headers }
})
);
if (errors) {
throw new Error(errors.map(({ message }: Error) => message).join("\n"));
}
if (!data) {
throw new Error("No data received from server introspection.");
}
return data.__schema;
};